Pagini recente » Cod sursa (job #290318) | Cod sursa (job #1392305) | Cod sursa (job #1587106) | Cod sursa (job #2333318) | Cod sursa (job #1453713)
#include <iostream>
#include <vector>
#include <fstream>
#define nmax 100005
using namespace std;
bool seen[nmax];
void get_data(int &n, vector<int> v[nmax]){
ifstream fin ("dfs.in");
int m, x, y;
fin >> n >> m;
for(int i= 0; i< m; i++){
fin >> x >> y;
v[x].push_back(y);
}
}
void dfs(int x, vector<int> v[nmax]){
seen[x]= true;
for(auto y: v[x]){
if(!seen[y]) dfs(y, v);
}
}
int main(){
ios_base::sync_with_stdio(false);
ofstream fout ("dfs.out");
vector<int> v[nmax];
int n, components= 0;
get_data(n, v);
for(int i=1; i<=n; i++)
if(!seen[i]) components++, dfs(i, v);
fout << components;
return 0;
}