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