Cod sursa(job #2309455)

Utilizator AndreiLunguLungu Andrei Sebastian AndreiLungu Data 29 decembrie 2018 00:17:10
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>
#define N 100004
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n , m , viz[N];
vector <int> L[N];
void Citire()
{
    int i , x , y;
    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
    fin.close();
}
void DFS(int nod)
{
    viz[nod] = 1;
    for(auto i : L[nod])
        if(viz[i] == 0)
            DFS(i);
}
void Rezolvare()
{
    int i , cnt;
    cnt = 0;
    for(i = 1; i <= n; i++)
        if(viz[i] == 0)
        {
            cnt++;
            DFS(i);
        }
    fout << cnt << "\n";
}
int main()
{
    Citire();
    Rezolvare();
    return 0;
}