Cod sursa(job #2144949)

Utilizator tanasaradutanasaradu tanasaradu Data 27 februarie 2018 00:06:32
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("dfs.in");
ofstream fout ("dfs.out");
const int Nmax =  100005;
vector < int > L[Nmax];
int dist[Nmax], n, m, s;
bool viz[Nmax];
void DFS(int varf)
{
    viz[varf] = true;
    for(auto i : L[varf])
        if(!viz[i])
            DFS(i);
}
int main()
{
    int x, y;
    fin >> n >> m;
    for(int i = 1 ; i <= m ; i++)
    {
        fin >> x >> y;
        L[x] . push_back(y);
        L[y] . push_back(x);
    }
    for(int i = 1 ; i <= n ; i++)
        if(!viz[i])
    {
        ++s;
        DFS(i);
    }
    fout << s << "\n";
    fin.close();
    fout.close();
    return 0;
}