Mai intai trebuie sa te autentifici.
Cod sursa(job #1753621)
| Utilizator | Data | 6 septembrie 2016 19:47:54 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.72 kb |
#include <fstream>
#include <cstdio>
#include <vector>
#define VAL 100005
using namespace std;
int N, M, i, x;
int a, b, ans;
bool ok[VAL];
vector<int> v[VAL];
void dfs(int x)
{
int i;
ok[x]=true;
for (i=0; i<v[x].size(); i++)
if (ok[v[x][i]]==false)
dfs(v[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", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
for (i=1; i<=N; i++)
{
if (ok[i]==false)
{
ans++;
dfs(i);
}
}
printf("%d\n", ans);
return 0;
}
