Pagini recente » Cod sursa (job #3225744) | Cod sursa (job #1966546) | Cod sursa (job #2893652) | Cod sursa (job #843546) | Cod sursa (job #2952525)
#include <iostream>
#include <vector>
#include <bitset>
#include <fstream>
using namespace std;
const int N = 1e5;
const int M = 2e5;
vector <int> a[N + 1];
bitset <N + 1> viz;
void dfs(int x){
viz[x] = 1;
for(auto y:a[x])
if(!viz[y])
dfs(y);
}
int main()
{
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n, m;
fin >> n >> m;
for(int i = 0; i < m; i++){
int x, y;
fin >> x >> y;
a[x].push_back(y);
}
int nrc = 0;
for(int i = 1; i <= n; i++){
if(!viz[i]){
nrc++;
dfs(i);
}
}
fout << nrc;
return 0;
}