Cod sursa(job #1974503)

Utilizator adiXMGemene Adrian adiXM Data 27 aprilie 2017 21:04:53
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
const int nMax = 100003;
struct Nod{
    int nod;
    Nod *leg;
};
Nod *G[nMax];
bool viz[nMax];
int comp;
inline void Add_edge(int x, int y) {
    Nod *p;
    p = new Nod;
    p->nod = y;
    p->leg = G[x];
    G[x] = p;
}
inline void Dfs(int node) {
    Nod *p;
    comp++;
    viz[node] = 1;
    for(p = G[node]; p != NULL; p = p->leg) {
        int j = p -> nod;
        if(!viz[j]) {
            Dfs(j);
        }
    }
}
int main()
{
    int n, m, x, y;
    Nod *p;
    f >> n >> m;
    for(int i = 1; i <= m; i++) {
        f >> x >> y;
        Add_edge(x, y);
        Add_edge(y, x);
    }
    Dfs(1);
    g << comp << "\n";
    return 0;
}