Cod sursa(job #934752)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 31 martie 2013 12:50:47
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include<stdio.h>
#include<vector>
using namespace std;
vector <int> v[100007];
int viz[100007];
int n , m , x , y , nr;
void dfs(int u)
{
    viz[u]=1;
    for(vector<int> :: iterator i=v[u].begin() ; i!=v[u].end() ; ++i)
        if(viz[*i] == 0)
            dfs(*i);
}
int main()
{
    freopen("dfs.in" , "r" , stdin);
    freopen("dfs.out" , "w" , stdout);
    scanf("%d %d" , &n , &m);
    for( ; m>0 ; --m)
    {
        scanf("%d %d" , &x , &y);
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(int i=1 ; i<=n ; ++i)
        if(viz[i] == 0)
            ++nr , dfs(i);
    printf("%d" , nr);
    return 0;
}