Cod sursa(job #2246306)
| Utilizator | Data | 26 septembrie 2018 22:06:14 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 10 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.63 kb |
#include <bits/stdc++.h>
using namespace std;
vector <int> graff[100001];
int parcurse[100001], nr = 0;
void dfs(int nod){
parcurse[nod] = 1;
for (auto x : graff[nod]){
if (parcurse[x] == 0){
parcurse[x] = 1;
}
}
}
int main() {
ifstream f("dfs.in");
ofstream g("dfs.out");
int n, m;
f >> n >> m;
for (int i = 1; i<=m ; i++){
int a, b;
f >> a >>b;
graff[a].push_back(b);
graff[b].push_back(a);
}
nr = 0;
for (int i = 1; i <= n ; i++){
if (!parcurse[i]){
nr ++;
dfs(i);
}
}
g <<nr;
return 0;
}