Cod sursa(job #151609)
| Utilizator | Data | 8 martie 2008 14:14:15 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.46 kb |
#include<fstream>
#include<vector>
using namespace std;
int n,m,viz[100001],i,nr,x,y;
vector<int> a[100001];
void df(int vf)
{
viz[vf]=1;
int size=a[vf].size();
for(int i=0;i<size;i++)
if(!viz[a[vf][i]])
df(a[vf][i]);
}
int main()
{
ifstream f("dfs.in");
ofstream g("dfs.out");
f>>n>>m;
for(i=1;i<=m;i++){
f>>x>>y;
a[x].push_back(y);
a[y].push_back(x);}
for(i=1;i<=n;i++)
if(!viz[i]){
nr++;
df(i);}
g<<nr;
g.close();
}