Pagini recente » Cod sursa (job #2307520) | Cod sursa (job #874395) | Cod sursa (job #1841820) | Cod sursa (job #305211) | Cod sursa (job #548420)
Cod sursa(job #548420)
#include<fstream.h>
#define NM 1000
ifstream fin("dfs.in");
ofstream out("dfs.out");
struct nod{ int nr;
nod *next;
};
nod *l[NM],*urm[NM];
int n,m,viz[NM],cc;
void add(nod *&l, int x){
nod *nn=new(nod);
nn->nr=x;
nn->next=l;
l=nn;
}
void build(){ int i,x,y;
fin>>n>>m;
for(i=0;i<m;i++){ fin>>x>>y;
add(l[x],y);
add(l[y],x);
}
}
void df(int x){ int y;
nod *nc=urm[x];
while(nc){ viz[x]=cc;
y=nc->nr;
nc=nc->next;
if(!viz[y])
df(y);
urm[x]=nc;
}
}
int main(){
int i;
build();
for(i=1;i<=n;i++) urm[i]=l[i];
for(i=1;i<=n;i++) if(!viz[i]){ cc++;
df(i);
}
out<<cc;
return 0;
}