Cod sursa(job #1189337)

Utilizator BlueStrutAndrei Prahoveanu BlueStrut Data 22 mai 2014 14:57:18
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include<cstdio>
#include<vector>
using namespace std;
vector<int> a[100001];
int i, n, m, x, y, cnt;
bool viz[100001];
void dfs(int poz){
    vector<int>::iterator it;
    viz[poz]=true;
    for (it=a[poz].begin();it!=a[poz].end();it++)
        if (viz[*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);
    }
    for (i=1;i<=n;i++)
        if (!viz[i]) {cnt++; dfs(i);}
    printf("%d\n", cnt);
    return 0;
}