Cod sursa(job #1109390)
| Utilizator | Data | 17 februarie 2014 09:07:35 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.48 kb |
#include <fstream>
#include <vector>
using namespace std;
ifstream fin ("dfs.in");
ofstream fout ("dfs.out");
const int nmax = 100002;
vector <int> a[nmax];
bool viz[nmax];
int n,m,x,y,i,j,sol;
void df (int k)
{
viz[k]=1;
for (i=1;i<=n;i++)
if (a[k][i]==1 && viz [i]==0)
df (i);
}
int main ()
{
fin>>n>>m;
for (i=1;i<=m;i++)
{
fin>>x>>y;
a[x][y]=1;
a[y][x]=1;
}
for (j=1;j<=n;j++)
if (viz[j]==0)
{ df (j);
++sol;}
fout<<sol;
return 0;
}
