Cod sursa(job #2212492)

Utilizator alexsandulescuSandulescu Alexandru alexsandulescu Data 14 iunie 2018 12:05:49
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("dfs.in");
ofstream g("dfs.out");

int N, M, x, y;
vector<int> G[100003];
map<int, bool> sel;
void df(int x) {
    sel[x] = true;
    for(auto it = G[x].begin(); it != G[x].end(); it++)
    if(!sel[*it]) {
        df(*it);
    }
}
int main()
{
    f >> N >> M;
    int nc = 0;
    for(int i = 1; i <= M; i++) {
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for(int i = 1; i <= N; i++) {
        if(!sel[i]) {
            nc++;
            df(i);
        }
    }
    g << nc << "\n";
    return 0;
}