Mai intai trebuie sa te autentifici.
Cod sursa(job #1830440)
Utilizator | Data | 16 decembrie 2016 18:52:10 | |
---|---|---|---|
Problema | Parcurgere DFS - componente conexe | Scor | 5 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.53 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;
int 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);
}
for(i = 0; i < n; i++)
if(!viz[i]) { df(i); componente++; }
g << componente << '\n';
return 0;
}