Cod sursa(job #1871027)

Utilizator horiacoolNedelcu Horia Alexandru horiacool Data 7 februarie 2017 03:09:45
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 2.45 kb
#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);
             M--;
             InserareStack(STp,STu,x,y);
         }
         else
         {
             STu->after = leg;

             if( leg != NULL )
                 leg->before = STu;

             leg = STu;

             STu = STu->before;

             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);

    if( M == 0 )
    while( STp->after != NULL )
    {
        g<<STp->y<<' ';
        STp = STp->after;
    }
    else
    g<<-1;

    return 0;
}