Cod sursa(job #2231160)

Utilizator TheNextGenerationAyy LMAO TheNextGeneration Data 13 august 2018 12:35:05
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>

using namespace std;
ifstream cin("dfs.in");
ofstream cout("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;
    cin >> n >> m;
    for (int i = 1; i<=m; i++)
    {
        int x,y;
        cin >> 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);
    cout << c;
}