Cod sursa(job #600196)

Utilizator andrianAndrian andrian Data 30 iunie 2011 19:28:12
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <iostream>
#include <fstream>
#include <vector>
#define nmax 100001
using namespace std;

int n, m, count=0;
vector<int> a[nmax];
int viz[nmax];

void dfs(int x){
    viz[x] = 1;
    for(unsigned int i=0;i<a[x].size();++i)
        if(!viz[a[x][i]])
            dfs(a[x][i]);
}

int main()
{
    int x, y;
    ifstream in("dfs.in");
    in >> n >> m;
    for(int i=1;i<=m;++i){
        in >> x >> y;
        a[x].push_back(y);
    }
    in.close();
    for(int i=1;i<=n;++i)
        if(!viz[i])
            {
                ++count;
                dfs(i);
            }
    ofstream out("dfs.out");
    out << count;
    out.close();
    return 0;
}