Cod sursa(job #2270591)

Utilizator Urdoi.BogdanUrdoi Bogdan Urdoi.Bogdan Data 27 octombrie 2018 11:42:29
Problema Parcurgere DFS - componente conexe Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
#include <list>

using namespace std;

ifstream f ("dfs.in");
ofstream g ("dfs.out");

list <int> graf[100001];
int n;
bool viz[100001];

void DFS(int k)
{
    viz[k]=1;
    list <int> :: iterator it = graf[k].begin();
    for(; it!=graf[k].end(); it++)
        if(!viz[*it])
            DFS(*it);
}

int main()
{
    int m, cnt=0;
    f >> n >> m;
    for(int i = 1, x, y; i <= n; i++){
        f >> x >> y;
        graf[x].push_back(y);
        graf[y].push_back(x);
    }
    for(int i = 1; i <= n; i++)
        if(!viz[i]){
            cnt++;
            DFS(i);
        }
    g << cnt;
    return 0;
}