Pagini recente » Cod sursa (job #2341937) | Cod sursa (job #2947269) | Cod sursa (job #1447624) | Cod sursa (job #628007) | Cod sursa (job #1491863)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
struct nod
{
int inf;
nod *urm;
}*l[100005];
int n,m;
int u[100005];
void adaugnod(nod *&p,int x)
{
nod *c;
c=new nod;
c->inf=x;
c->urm=p;
p=c;
}
void citire()
{
int i;
int x,y;
f>>n>>m;
for (i=1;i<=m;i++)
{
f>>x>>y;
adaugnod(l[x],y);
adaugnod(l[y],x);
}
}
void df(int inf)
{
nod *p;
u[inf]=1;
for (p=l[inf];p!=NULL;p=p->urm)
{
if (!u[p->inf])
df(p->inf);
}
}
int main()
{
citire();
int nr=0,i;
for (i=1;i<=n;i++)
{
if (!u[i])
{
nr++;
df(i);
}
}
g<<nr;
return 0;
}