Pagini recente » Cod sursa (job #668558) | Cod sursa (job #3166538) | Cod sursa (job #295476) | Cod sursa (job #2732805) | Cod sursa (job #544997)
Cod sursa(job #544997)
#include<fstream.h>
#define nmax 100011
ifstream f("dfs.in");
ofstream g("dfs.out");
long long n,m,viz[100];
struct nod{
int c;
nod *next;
};
nod *a[100];
void add(int x,int y){
nod *q=new nod;
q->c=y;
q->next=a[x];
a[x]=q;
};
void citire(){
int x,y;
f>>n>>m;
for(int i=1;i<=m;i++)
f>>x>>y,add(x,y);
}
void dfs(int j){
viz[j]=1;
nod *q=new nod;
q=a[j];
while(q){
if(viz[q->c]==0)
dfs(q->c);
q=q->next;
}
}
int main(){
int k=0;
citire();
for(int i=1;i<=n;i++)
if(viz[i]==0)
dfs(i),++k;
g<<k<<'\n';
g.close();
return 0;
}