Cod sursa(job #628897)
| Utilizator | Data | 2 noiembrie 2011 12:00:44 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 5 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.58 kb |
#include<fstream>
#include<vector>
#include<stack>
using namespace std;
vector <int> v[100005];
bool a[100005];
int n,nr;
long long m, i, p, b;
void DFS(int x)
{
a[x] = true;
for(i=0;i<v[x].size();i++)
if (a[v[x][i]]==false)
DFS(v[x][i]);
}
int main()
{
ifstream f("dfs.in");
ofstream h("dfs.out");
f>>n>>m;
for (i=1; i<=m; i++)
{
f>>p>>b;
v[p].push_back(b);
}
for (i=0;i<n;i++)
if (a[i]==false)
{
DFS(i);
nr=nr+1;
}
h<<nr<<'\n';
f.close();
h.close();
return 0;
}