Mai intai trebuie sa te autentifici.
Cod sursa(job #1705397)
Utilizator | Data | 20 mai 2016 15:17:12 | |
---|---|---|---|
Problema | Parcurgere DFS - componente conexe | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.61 kb |
#include <bits/stdc++.h>
using namespace std;
int m,n,i,nc,x,y;
bool sel[100010];
vector<int>g[100010];
void DFS(int x)
{
int i;
sel[x]=true;
for(i=0; i<g[x].size(); ++i)
if(!sel[g[x][i]])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);
}
for(i=1; i<=n; ++i)
if(!sel[i])
{
DFS(i);
nc++;
}
printf("%d\n",nc);
return 0;
}