Mai intai trebuie sa te autentifici.
Cod sursa(job #1192637)
| Utilizator | Data | 29 mai 2014 14:22:06 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.67 kb |
#include <cstdio>
#include <vector>
using namespace std;
int i,n,m,x,nr,y;
bool sel[100004];
vector<int> g[100004];
void dfs (int x)
{
int i;
sel [x]=true;
for (i=0;i<g[x].size();i++)
if (sel[g[x][i]]==false)
dfs (g[x][i]);
}
int main()
{
freopen ("dfs.in","r",stdin);
freopen ("dfs.out","w",stdout);
scanf ("%d %d", &n, &m);
for (i=1;i<=m;i++)
{
scanf ("%d %d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
nr=0;
for (i=1;i<=n;i++)
if (sel[i]==false)
{
dfs(i);
nr++;
}
printf ("%d", nr);
return 0;
}
