Cod sursa(job #780755)

Utilizator my666013Test Here my666013 Data 21 august 2012 11:24:50
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define Max 100001

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

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

int main(){
    int a,b,m;
    freopen("dfs.in","r",stdin);
    freopen("dfs.out","w",stdout);

    scanf("%d %d",&n,&m);

    while(m--)
    {
        scanf("%d %d",&a,&b);
        g[a].push_back(b);
        g[b].push_back(a);
    }

    for(int i=1;i<=n;i++)
    if( !was[i] )
    {
        nr++;
        dfs(i);
    }

    printf("%d\n",nr);

    return 0;
}