Cod sursa(job #1507520)

Utilizator torckySuciu Victor torcky Data 21 octombrie 2015 18:32:42
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <cstdio>
#include <vector>
using namespace std;
const int NMAX=100000;
vector <int> G[NMAX+1];
int viz[NMAX+1];
int CC;
void dfs(int u){
    viz[u]=CC;
    for(int i=0;i<(int)G[u].size();i++){
        int v=G[u][i];
        if(!viz[v]){
            dfs(v);
        }
    }
}
int main()
{
    freopen("dfs.in","r",stdin);
    freopen("dfs.out","w",stdout);
    int n,m,u,v,i;
    scanf("%d%d",&n,&m);
    for(i=1;i<=m;i++){
        scanf("%d%d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    for(i=1;i<=n;i++){
        if(!viz[i]){
            CC++;
            dfs(i);
        }
    }
    printf("%d\n",CC);
    return 0;
}