Pagini recente » Cod sursa (job #1366033) | Cod sursa (job #676328) | Cod sursa (job #2489330) | Cod sursa (job #371740) | Cod sursa (job #1265440)
#include <cstdio>
using namespace std;
struct nod{int x;nod *urm;};
nod *a[100001];
int n,m,use[100001],i,x,y,k;
void add(nod *&p,int b)
{nod *t;
t=new nod;
t->x=b;
t->urm=p;
p=t;
}
void dfs(int b)
{nod *p;
use[b]=1;
for (p=a[b];p;p=p->urm) if (!use[p->x]) dfs(p->x);
}
int main()
{FILE *f1 = fopen("dfs.in","r");
FILE *f2 = fopen("dfs.out","w");
fscanf(f1,"%d%d",&n,&m);
for (i=1;i<=m;i++) {fscanf(f1,"%d%d",&x,&y);
add(a[x],y);add(a[y],x);}
for (i=1;i<=n;i++) if(!use[i]) {k++;dfs(i);}
fprintf(f2,"%d",k);
fclose(f1);fclose(f2);
return 0;
}
//Challenges are what make life interesting and overcoming them is what makes life meaningful.