Cod sursa(job #3370769)
| Utilizator | Data | 27 octombrie 2026 11:15:35 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.6 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n, x, i, j, m;
vector<int> G[100005];
vector<bool> v;
void DFS(int X)
{
v[X] = 1;
for(auto I = G[X].begin(); I != G[X].end(); I++)
if(v[*I] == 0)
DFS(*I);
}
int main()
{
fin >> n >> m;
v = vector<bool>(n + 1);
while(fin >> i >> j)
{
G[i].push_back(j);
G[j].push_back(i);
}
for(int i = 1; i <= n; ++i)
if(v[i] == 0)
{
x++;
DFS(i);
}
fout << x;
return 0;
}