Cod sursa(job #3368458)
| Utilizator | Data | 14 octombrie 2026 17:05:40 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.62 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n, m;
vector <int> ad[100005];
int viz[100005];
void DFS(int nod)
{
viz[nod] = 1;
for(int e : ad[nod])
if(viz[e] == 0)
DFS(e);
}
int main()
{
int x, y;
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
fin >> x >> y;
ad[x].push_back(y);
ad[y].push_back(x);
}
x = 0;
for(int i = 1; i <= n; i++)
if(viz[i] == 0)
{
x++;
DFS(i);
}
fout << x << '\n';
return 0;
}
