Pagini recente » Cod sursa (job #1768437) | Cod sursa (job #2358690) | Cod sursa (job #2388494) | Cod sursa (job #2967910) | Cod sursa (job #215683)
Cod sursa(job #215683)
#include <iostream.h>
#include <fstream.h>
ifstream fin("dfs.in");
ofstream fout("dfs.out");
struct nod{long info; nod *urm;};
char v[100005];
long n,nc,m,i;
nod *l[100005];
void add(nod *&p, long x)
{nod *c;
c=new nod;
c->info=x;
c->urm=0;
if (!p) p=c;
else
{c->urm=p;
p=c;
}
}
void dfs(long i)
{nod *c;
v[i]=1;
c=l[i];
while (c)
{if (v[c->info]==0)
dfs(c->info);
c=c->urm;
}
}
int main()
{long x,y;
fin>>n>>m;
for (i=1;i<=m;i++)
{fin>>x>>y;
add(l[x],y);
add(l[y],x);
}
for (i=1;i<=n;i++)
if (v[i]==0)
{nc++; dfs(i);
}
fout<<nc;
fout.close();
return 0;
}