Cod sursa(job #1121113)
| Utilizator | Data | 25 februarie 2014 11:36:52 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.6 kb |
#include <cstdio>
#include <vector>
using namespace std;
int N,M;
vector<int> V[100005];
int us[100005];
int SOL=0;
void D(int i){
us[i]=1;
for( vector<int>::iterator it=V[i].begin();it!=V[i].end(); ++it)
if( ! us[*it] ) D(*it);
}
int main()
{
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
scanf("%d%d",&N,&M);
int x,y;
while( M-- ){
scanf("%d%d",&x,&y);
V[x].push_back(y);
V[y].push_back(x);}
for(register int i=1;i<=N;++i )
if( !us[i] )
++SOL,D(i);
printf("%d",SOL);
return 0;
}
