Nu exista pagina, dar poti sa o creezi ...
Cod sursa(job #1705838)
| Utilizator | Data | 21 mai 2016 00:02:52 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.57 kb |
#include <vector>
#include <fstream>
#include <queue>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
vector <int> graph[100005];
vector <bool> viz(100005, false);
void dfs(int node){
viz[node] = true;
int son;
for(int i = 0; i < graph[node].size(); i++){
son = graph[node][i];
if(!viz[son])
dfs(son);
}
}
int main(){
int n, m;
f >> n >> m;
int x, y;
for(int i = 1; i <= m; i++){
f >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
int nr = 0;
for(int i = 1; i <= n; i++){
if(!viz[i]){
dfs(i);
nr ++;
}
}
g << nr;
}