Cod sursa(job #1830441)

Utilizator snappyRazvan Alin snappy Data 16 decembrie 2016 18:53:42
Problema Parcurgere DFS - componente conexe Scor 35
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>
using namespace std;

const int N = 100005;

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

vector<int> v[N];
bool viz[N];
int n, m, x, y, i, componente;

void df(int x)
{
    viz[x] = 1;
    for(auto it : v[x])
        if(!viz[it])
            df(it);
}

int main()
{
    f >> n >> m;
    for(i = 0; i < n; i++) {
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    for(i = 0; i < n; i++)
        if(!viz[i]) { df(i); componente++; }

    g << componente << '\n';
    return 0;
}