Cod sursa(job #1905449)

Utilizator tudorgalatanRoman Tudor tudorgalatan Data 6 martie 2017 08:11:14
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <fstream>
#include <vector>

using namespace std;

inline void DFS (unsigned int node);

ifstream fin ("dfs.in");
ofstream fout ("dfs.out");

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

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

unsigned int solution;

int main ()
{
    fin >> N >> M;
    for (i=1; i<=M; i++)
    {
        fin >> X >> Y;
        G[X].push_back(Y);
        G[Y].push_back(X);
    }
    for (i=1; i<=N; i++)
        if (!seen[i])
        {
            DFS(i);
            solution++;
        }
    fout << solution;
    return 0;
}

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