Cod sursa(job #2270501)

Utilizator SCatalinStanciu Catalin SCatalin Data 27 octombrie 2018 11:20:36
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
const int N = 1e5+5;
vector<int> v[N];
bool viz[N];

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;
}