Cod sursa(job #1830446)
Utilizator | Data | 16 decembrie 2016 18:59:31 | |
---|---|---|---|
Problema | Parcurgere DFS - componente conexe | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.55 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(; m; m--) {
f >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
for(i = 1; i <= n; i++)
if(!viz[i]) { componente++; df(i); }
g << componente << '\n';
return 0;
}