Cod sursa(job #2197191)

Utilizator alexsandulescuSandulescu Alexandru alexsandulescu Data 21 aprilie 2018 12:59:45
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>

using namespace std;

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

vector<int> graf[100003];

int N, M, x, y, cnt, visit[100003];
void df(int x) {
    visit[x] = true;
    for(auto i = graf[x].begin(); i != graf[x].end(); i++) {
        if(!visit[*i]) df(*i);
    }
}

int main()
{
    f >> N >> M;
    for(int i = 1; i <= M; i++)
        f >> x >> y, graf[x].push_back(y), graf[y].push_back(x);
    for(int i = 1; i <= N; i++)
        if(visit[i] == false)
            df(i), cnt++;
    g << cnt << "\n";
    return 0;
}