Cod sursa(job #788555)

Utilizator round2Test P round2 Data 15 septembrie 2012 13:03:39
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <cstdio>
#include <vector>
using namespace std;
#define Max 100001
#define pb push_back

vector<int>v[Max];
int n,nr;
bool was[Max];

void dfs(int x)
{
    int y;
    was[x]=1;
    for(int i=0;i<v[x].size();i++)
    {
        y=v[x][i];
        if(was[y]==0)dfs(y);
    }
}


int main()
{
    int m,x,y;
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
        scanf("%d %d",&n,&m);
        while(m--)
        {
            scanf("%d %d",&x,&y);
            v[x].pb(y);
            v[y].pb(x);
        }
        for(int i=1;i<=n;i++)
        if(was[i]==0)
        {
            nr++;
            dfs(i);
        }
        printf("%d\n",nr);
    return 0;
}