Cod sursa(job #779285)

Utilizator visanrVisan Radu visanr Data 17 august 2012 13:26:50
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;


#define nmax 100010
#define pb push_back

int N, M, used[nmax], sol, X, Y;
vector<int> G[nmax];


void DFS(int node);

int main()
{
    freopen("dfs.in", "r", stdin);
    freopen("dfs.out", "w", stdout);
    int i;
    scanf("%i %i", &N, &M);
    for(; M; M --)
    {
          scanf("%i %i", &X, &Y);
          G[X].pb(Y);
          G[Y].pb(X);
    }
    for(i = 1; i <= N; i++)
          if(!used[i])
                      sol ++, DFS(i);
    printf("%i\n", sol);
    scanf("%i", &i);
    return 0;
}

void DFS(int node)
{
     for(vector<int> :: iterator it = G[node].begin(); it != G[node].end(); ++ it)
                     if(!used[*it])
                                   used[*it] = 1, DFS(*it);
}