Mai intai trebuie sa te autentifici.
Cod sursa(job #2952525)
| Utilizator | Data | 9 decembrie 2022 15:09:09 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 15 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.68 kb |
#include <iostream>
#include <vector>
#include <bitset>
#include <fstream>
using namespace std;
const int N = 1e5;
const int M = 2e5;
vector <int> a[N + 1];
bitset <N + 1> viz;
void dfs(int x){
viz[x] = 1;
for(auto y:a[x])
if(!viz[y])
dfs(y);
}
int main()
{
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n, m;
fin >> n >> m;
for(int i = 0; i < m; i++){
int x, y;
fin >> x >> y;
a[x].push_back(y);
}
int nrc = 0;
for(int i = 1; i <= n; i++){
if(!viz[i]){
nrc++;
dfs(i);
}
}
fout << nrc;
return 0;
}
