Cod sursa(job #1379782)

Utilizator supremAlex Imbrea suprem Data 6 martie 2015 19:26:31
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <fstream>
#include <vector>
#define INF 1<<30
#define NMAX 100001
using namespace std;

ifstream f("dfs.in");
ofstream g("dfs.out");


vector< vector<int> > v(NMAX);
vector<bool> viz(NMAX, false);

void dfs(int x)
{
    viz[x]= true;
   // cout<<x<<" ";
    for(int i=0; i<v[x].size(); i++)
    {
        if(viz[v[x][i]] == false)
            dfs(v[x][i]);
    }

}
int main()
{
    int x, y, c;
    int n,m;
    f>>n>>m;

    while(f>>x>>y) v[x].push_back(y);

    int nr=0;
    for(int i=1; i<=n; ++i)
    {

        if(viz[i] == false)
        {
           // cout<<"Componenta conexa "<<++nr<<"\n";
            ++nr;
            dfs(i);

            //cout<<"\n";
        }

    }

    return 0;
}