Cod sursa(job #2673427)

Utilizator RazvanucuPopan Razvan Calin Razvanucu Data 16 noiembrie 2020 19:31:49
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");

vector <int> G[100005];
bool viz[100005];
int n,m,x,y,cnt;
void DFS(int nod_curent)
{
    viz[nod_curent]=true;
    for(vector<int>::iterator nod_vecin=G[nod_curent].begin(); nod_vecin!=G[nod_curent].end(); ++nod_vecin)
        if(!viz[*nod_vecin])
            DFS(*nod_vecin);
}
int main()
{
    f>>n>>m;
    while(f>>x>>y)
    {
        G[x].push_back(y);
        G[y].push_back(x);
    }

    for(int i=1; i<=n; i++)
        if(!viz[i])
        {
            cnt++;
            DFS(i);
        }

    g<<cnt;
    return 0;
}