Pagini recente » Statisticile problemei Autobuze | Cod sursa (job #1368052) | Cod sursa (job #182885) | Cod sursa (job #1832556) | Cod sursa (job #1731144)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
queue<unsigned short int> G[100001];
int n,m;
bool viz[100001];
void read()
{
f>>n>>m;
int a,b;
for(int i=1;i<=m;i++)
{
f>>a>>b;
G[a].push(b);
}
}
void dfs(int source)
{
while(!G[source].empty())
{
int t= G[source].front();
G[source].pop();
if(viz[t]==false)
{
viz[t]=true;
dfs(t);
}
}
}
int nrComp;
int main()
{
read();
for(int i=1;i<=n;i++)
if(viz[i]==false)
{
viz[i]=true;
nrComp++;
dfs(i);
}
g<<nrComp;
f.close();
g.close();
return 0;
}