Cod sursa(job #1226264)

Utilizator BlueStrutAndrei Prahoveanu BlueStrut Data 4 septembrie 2014 23:00:47
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.58 kb
#include<cstdio>
#include<vector>
using namespace std;
vector<int> a[100003];
int i, n, m, x, y, nr;
bool ok[100003];
void dfs(int x){
    vector<int>::iterator it;
    ok[x]=true;
    for (it=a[x].begin();it!=a[x].end();it++)
        if (ok[*it]==false) dfs(*it);
}
int main(){
    freopen("dfs.in","r",stdin);
    freopen("dfs.out","w",stdout);
    scanf("%d%d", &n, &m);
    for (i=1;i<=m;i++) {scanf("%d%d", &x, &y); a[x].push_back(y); a[y].push_back(x);}
    nr=0;
    for (i=1;i<=n;i++)
        if (ok[i]==false) {nr++; dfs(i);}
    printf("%d\n", nr); return 0;
}