Cod sursa(job #2231161)

Utilizator TheNextGenerationAyy LMAO TheNextGeneration Data 13 august 2018 12:35:30
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 in("dfs.in");
ofstream out("dfs.out");
const int NMAX = 1e5+5;
vector<int> v[NMAX];
bool viz[NMAX];
void dfs(int x)
{
    viz[x] = 1;
    for (auto it: v[x])
        if (!viz[it])
            dfs(it);
}

int main()
{
    int n,m;
    in >> n >> m;
    for (int i = 1; i<=m; i++)
    {
        int x,y;
        in >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    int c = 0;
    for (int i = 1; i<=n; i++)
        if (!viz[i]) c++, dfs(i);
    out << c;
}