Cod sursa(job #1774327)

Utilizator tudorgalatanRoman Tudor tudorgalatan Data 8 octombrie 2016 20:07:41
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>
#include <vector>

using namespace std;

void DFS (unsigned int node);

unsigned int N, M;
unsigned int X, Y;

vector < unsigned int > G[100001];
bool seen[100001];
unsigned int i;

unsigned int sol;

int main ()
{
    ifstream fin ("dfs.in");
    fin >> N >> M;
    for (i=1; i<=M; i++)
    {
        fin >> X >> Y;
        G[X].push_back(Y);
        G[Y].push_back(X);
    }
    fin.close();
    for (i=1; i<=N; i++)
        if (!seen[i])
        {
            sol++;
            DFS(i);
        }
    ofstream fout ("dfs.out");
    fout << sol;
    fout.close();
    return 0;
}

void DFS (unsigned int node)
{
    unsigned int i;
    seen[node] = 1;
    for (i=0; i<G[node].size(); i++)
        if (!seen[G[node][i]])
            DFS(G[node][i]);
}