Cod sursa(job #1240148)

Utilizator andreey_047Andrei Maxim andreey_047 Data 10 octombrie 2014 16:41:38
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <iostream>
#define in "dfs.in","r",stdin
#define out "dfs.out","w",stdout
#include <cstdio>
#include <vector>
#define nmax 100009
using namespace std;
int n,m,viz[nmax],nr;
vector<int>G[nmax];
void dfs(int x){
 if(!viz[x]){
      viz[x]=1;
    for(int i = 0 ; i < G[x].size();i++)
      dfs(G[x][i]);
 }
}
int main(){
    int x,y;
    freopen(in);
    freopen(out);
    scanf("%d%d",&n,&m);
    while(m--){
        scanf("%d%d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for(int i=1;i<=n;i++)
    if(!viz[i]){
        nr++;
        dfs(i);
    }
    cout << nr <<'\n';
    return 0;
}