Pagini recente » Cod sursa (job #2407268) | Cod sursa (job #1744101) | Cod sursa (job #938034) | Cod sursa (job #3241733) | Cod sursa (job #1872567)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int N,M,u;
struct nod
{
int info;
nod* urm;
}*pt[100003];
struct Stack
{
int x,y;
Stack *before,*after;
}*STp,*STu,*leg;
void InserareNod(nod* &point,int val)
{
nod* cap = new nod;
cap->info = val;
cap->urm = point;
point = cap;
}
void StergereNod(nod* &point,int val)
{
nod*desters,*cap = point;
if( cap != NULL )
if( cap->info == val )
{
desters = cap;
point = cap->urm;
delete desters;
return ;
}
while( cap != NULL )
{
if( cap->urm != NULL )
if( cap->urm->info == val )
{
desters = cap->urm;
cap->urm = cap->urm->urm;
delete desters;
return ;
}
cap = cap->urm;
}
}
void InserareStack(Stack* &first,Stack* &last,int x,int y)
{
Stack* cap = first;
if( cap == NULL )
{
cap = new Stack;
cap->x = x;
cap->y = y;
cap->after = NULL;
cap->before = NULL;
first = last = cap;
}
else
{
cap = new Stack;
cap->x = x;
cap->y = y;
cap->after = NULL;
cap->before = last;
last->after = cap;
last = cap;
}
}
void DF(int xp)
{
int x,y;
STp = STu = leg = NULL;
InserareStack(STp,STu,0,xp);
u = 1;
while( u != 0 )
{
x = STu->y;
if( pt[x] != NULL )
{
y = pt[x]->info;
StergereNod(pt[x],y);
StergereNod(pt[y],x);
u++;
InserareStack(STp,STu,x,y);
}
else
{
STu->after = leg;
if( leg != NULL )
leg->before = STu;
leg = STu;
STu = STu->before;
if( STu != NULL )
STu->after = NULL;
u--;
}
}
}
int main()
{
int a,b;
f>>N>>M;
for(int i = 1 ; i <= N ; i++)
pt[i] = NULL;
for(int i = 1 ; i <= M ; i++)
{
f>>a>>b;
InserareNod(pt[a],b);
InserareNod(pt[b],a);
}
DF(1);
while( STp->after != NULL )
{
g<<STp->y<<' ';
STp = STp->after;
}
return 0;
}