Cod sursa(job #2549838)
| Utilizator | Data | 18 februarie 2020 06:47:52 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.66 kb |
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n, m, cnt;
vector<int> G[100001];
bool V[100001];
void Dfs(int x)
{
V[x] = true;
for (int y : G[x])
{
if (!V[y])
Dfs(y);
}
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
int x, y;
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
for (int i = 1; i <= n; ++i)
{
if (!V[i])
{
++cnt;
Dfs(i);
}
}
fout << cnt;
return 0;
}
