Pagini recente » Cod sursa (job #364563) | Cod sursa (job #753944) | Cod sursa (job #2232181) | Cod sursa (job #462839) | Cod sursa (job #921851)
Cod sursa(job #921851)
#include <iostream>
#include<fstream>
using namespace std;
struct nod
{
int vf;
nod *next;
}*p[100003],*k,*nou;
int viz[100003];
int main()
{
fstream f("dfs.in",ios::in), g("dfs.out",ios::out);
int n,m,i,j,nrC=0,x,y;
f>>n>>m;
for(i=1;i<=n;i++)
{
f>>x>>y;
nod *nou;
nou=new nod;
nou->vf=y;
nou->next=p[x];
p[x]=nou;
nou=new nod;
nou->vf=x;
nou->next=p[y];
p[y]=nou;
}
int vfcrt=1,gasit,vecin;
do
{
nrC++;
viz[vfcrt]=1;
k=new nod;
k->vf=vfcrt;
k->next=NULL;
while(k!=NULL)
{
vfcrt=k->vf;
cout<<vfcrt<<" ";
vecin=0;
while(p[vfcrt]!=NULL)
if(viz[p[vfcrt]->vf]==0)
{
nou=new nod;
nou->vf=p[vfcrt]->vf;
viz[nou->vf]=1;
nou->next=k;
k=nou;
vecin=1;
break;
}
else
p[vfcrt]=p[vfcrt]->next;
if(vecin==0)
k=k->next;
}
gasit=0;
for(i=1;i<=n;i++)
if(viz[i]==0)
{
gasit=1;
vfcrt=i;
}
}
while(gasit);
g<<nrC;
}