Pagini recente » Cod sursa (job #3207678) | Cod sursa (job #546395) | Cod sursa (job #1161833) | Cod sursa (job #2508234) | Cod sursa (job #585369)
Cod sursa(job #585369)
#include <cstdio>
#include <iostream>
#include <vector>
#include <bitset>
using namespace std;
const int MAX_NODURI = 100000;
int nNoduri, nMuchii;
vector<int> muchii[MAX_NODURI];
bitset<MAX_NODURI> isNodVizitat;
void dfs(int nod){
vector<int>::iterator vecin;
for(vecin = muchii[nod].begin(); vecin<muchii[nod].end(); ++vecin)
if(!isNodVizitat[*vecin]){
isNodVizitat[*vecin] = true;
dfs(*vecin);
}
}
int main()
{
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
cin >> nNoduri >> nMuchii;
for (int i=0;i<nMuchii;++i){
static int x,y;
cin >> x >> y;
muchii[x].push_back(y);
}
int nComponente = 0;
for (int i=1; i<=nNoduri; ++i)
if (!isNodVizitat[i]){
++nComponente;
dfs(i);
}
cout << nComponente << '\n';
return 0;
}