Cod sursa(job #2271745)

Utilizator PushkinPetolea Cosmin Pushkin Data 29 octombrie 2018 10:16:10
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
vector<vector<int>> G;
unsigned int n, res=0;
vector<bool> viz;
void rd()
{
    int x, y;
    f>>n>>res;
    res=0;
    viz.resize(n+3);
    G.resize(n+3);
    while(f>>x>>y)
    {
        G[x].push_back(y);
        G[y].push_back(x);
    }
}
void DFS(int x)
{
    viz[x]=1;
    for(auto a:G[x])
        if(!viz[a])
            DFS(a);
}
int main()
{
    rd();
    for(unsigned int i=1;i<=n;i++)
        if(!viz[i])
        {
            DFS(i);
            res++;
        }
    g<<res;
    f.close();
    g.close();
    return 0;
}