Cod sursa(job #2777601)
| Utilizator | Data | 23 septembrie 2021 19:11:09 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.64 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
const int N = 1e5 + 1;
int n, m, x, y, cnt;
vector<int> c[N];
bool vis[N];
void dfs(int nod){
vis[nod] = 1;
for(int i = 0; i < c[nod].size(); i++){
if(!vis[c[nod][i]])
dfs(c[nod][i]);
}
}
int main(){
f >> n >> m;
for(int i = 0; i < m; i++){
f >> x >> y;
c[x].push_back(y);
c[y].push_back(x);
}
f.close();
for(int i = 1; i <= n; i++){
if(!vis[i]){
cnt++;
dfs(i);
}
}
g << cnt;
g.close();
}
