Cod sursa(job #1249583)

Utilizator deea101Andreea deea101 Data 27 octombrie 2014 11:02:33
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <fstream>
#include <vector>
#define NMAX 100001
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");

vector <int> G[NMAX];

int N,M,comp[NMAX],count;

void dfs (int nod)
{
    comp[nod]=count;

    int i;
    for(i=0;i<G[nod].size();i++)
        if(!comp[G[nod][i]])
            dfs(G[nod][i]);

}
int main()
{
    f>>N>>M;
    int x,y;
    while(M--)
    {
        f>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    for( int i=1; i<=N; i++)
        if(!comp[i])
        {
            count++;
            dfs(i);
        }

    g<<count;
}