Cod sursa(job #2211944)

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

using namespace std;

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

int N, M, x, y, cer;
vector<int> graph[100003];
map<int, int> visit;
void dfs(int x) {
    visit[x] = true;
    for (int &i : graph[x]) {
        if(!visit[i]) dfs(i);
    }
}

int main()
{
    f >> N >> M;
    for(int i = 1; i <= M; i++) {
        f >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    for(int i = 1; i <= N; i++)
        if(!visit[i]) {
            dfs(i);
            cer++;
        }
    g << cer << "\n";
    return 0;
}