Cod sursa(job #1463251)
| Utilizator | Data | 20 iulie 2015 17:15:10 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 10 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.54 kb |
#include <vector>
#include <fstream>
#include <iostream>
#define nmax 50005
using namespace std;
bool seen[nmax];
vector<int> v[nmax];
int n, m, components, x, y;
ifstream fin ("dfs.in");
ofstream fout ("dfs.out");
void dfs(int x){
seen[x]= true;
for(auto i: v[x])
if(!seen[i]) dfs(i);
}
int main(){
fin >> n >> m;
for(int i=1; i<=m; i++){
fin >> x >> y;
v[x].push_back(y);
}
for(int i=1; i<=n; i++)
if(!seen[i]) dfs(i), components++;
fout << components;
}
