Mai intai trebuie sa te autentifici.
Cod sursa(job #3334998)
| Utilizator | Data | 21 ianuarie 2026 01:51:29 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 1.07 kb |
#include fstream
#include vector
#include stack
int main()
{
int n, m;
stdvectorstdvectorint la;
stdifstream in(dfs.in);
in n m;
for (int i = 0; i n; i ++) {
la.push_back(stdvectorint());
}
for (int i = 0; i m; i ++) {
int x, y;
in x y;
x --;
y --;
la[x].push_back(y);
}
in.close();
stdvectorbool este_parcurs(n, false);
int nr_comp_conexe = 0;
for (int i = 0; i n; i ++) {
if (este_parcurs[i]) {
continue;
}
nr_comp_conexe ++;
stdstackint s;
s.push(i);
este_parcurs[i] = true;
while (!s.empty()) {
int act = s.top();
s.pop();
for (auto vec la[act]) {
if (este_parcurs[vec]) {
continue;
}
este_parcurs[vec] = true;
s.push(vec);
}
}
}
stdofstream out(dfs.out);
out nr_comp_conexe;
out.close();
return 0;
}