Cod sursa(job #1328872)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 28 ianuarie 2015 20:43:37
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>
#include <vector>

#define Nmax 100100
#define neighbour Graph[node][i]

using namespace std;

vector <int> Graph[Nmax];
int N, Answer;
bool seen[Nmax];

void Dfs(int node) {

    seen[node] = true;

    for(int i = 0; i < Graph[node].size(); i++)
        if(!seen[neighbour])
            Dfs(neighbour);
}
void Solve() {

    for(int i = 1; i <= N; i++)
        if(!seen[i]) {
            ++Answer;
            Dfs(i);
        }
}
void Read() {

    int x, y, M;
    ifstream in("dfs.in");

    in >> N >> M;
    while(M--) {
        in >> x >> y;
        Graph[x].push_back(y);
        Graph[y].push_back(x);
        }

    in.close();
}
void Write() {

    ofstream out("dfs.out");
    out << Answer << '\n';
    out.close();
}
int main() {

    Read();
    Solve();
    Write();

    return 0;
}