Pagini recente » Cod sursa (job #949721) | Cod sursa (job #556108) | Cod sursa (job #1841995) | Cod sursa (job #2985026) | Cod sursa (job #277641)
Cod sursa(job #277641)
#include<fstream.h>
#define max 100000
#define maxm max*(max-1)/2
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n,m,v[max],nc;
struct nod {int info;
nod *urm;
};
nod *l[max];
void citire()
{int i,x,y;
fin>>n>>m;
for(i=1;i<=m;i++)
{fin>>x>>y;
nod *d;
d=new nod;
d->info=y;
d->urm=l[x];
l[x]=d;
}
}
void dfs(int i)
{v[i]=1;
nod *d;
d=l[i];
while(d)
{if(v[d->info]==0) dfs(d->info);
d=d->urm;
}
}
int main()
{int i; nc=0;
citire();
for(i=1;i<=n;i++)
if(v[i]==0) {dfs(i);
nc++;
}
fout<<nc;
fin.close();
fout.close();
return 0;
}