Cod sursa(job #2660677)

Utilizator Harsa_AndreiHarsa Andrei Harsa_Andrei Data 20 octombrie 2020 08:41:54
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.55 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("dfs.in");
ofstream fout("dfs.out");

list<int> M[100002];

int n, m, k;

int viz[100002];

void DFS(int x)
{
    viz[x] = k;
    for(int y : M[x])
        if(!viz[y])
            DFS(y);
}

int main()
{
    int x, y;
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        fin >> x >> y;
        M[x].push_back(y);
        M[y].push_back(x);
    }

    for(int i = 1; i <= n; i++)
        if(!viz[i])
        {
            k++;
            DFS(i);
        }
    fout << k;
    return 0;
}