Cod sursa(job #1204675)

Utilizator Cristian1997Vintur Cristian Cristian1997 Data 3 iulie 2014 16:58:39
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 3.01 kb
using namespace std;
#include <fstream>
#include <vector>
#include <stack>
ofstream fout("ciclueuler.out");
const int Nmax = 100000;
struct lista {int x; lista* urm;} ;

int m, n;
lista* v[Nmax+1];
vector<int> sol;
int deg[Nmax+1];
bool uz[Nmax+1];
stack<int> S;

void read() ;
int DFS(int) ;
int esteEulerian() ;
void creareLant(int) ;
void push1(int, int) ;

int main()
{
    read();
    if( !esteEulerian() ) {fout << "-1\n"; return 0;}
    S.push(1);
    while( !S.empty() )
    {
        creareLant(S.top());
        sol.push_back(S.top());
        S.pop();
    }
    vector<int>::iterator it = sol.begin(); ++it;
    for(; it != sol.end(); ++it)
        fout << *it << ' ';
    fout << '\n';
    return 0;
}


void read()
{
    ifstream fin("ciclueuler.in");
    int a, b;
    fin >> n >> m;
    for(; m; --m)
    {
        fin >> a >> b;
        ++deg[a]; ++deg[b];
        push1(a, b);
        if(a != b) push1(b, a);
    }
    fin.close();
}


int DFS(int vf)
{
    int nr = 1, ok; uz[vf] = 1;
    lista *nod;
    for(S.push(vf); !S.empty(); )
    {
        vf = S.top(); ok = 0;
        for(nod = v[vf]; nod != NULL && !ok; nod = nod->urm)
            if(!uz[nod->x])
            {
                S.push(nod->x);
                uz[nod->x] = 1; ok = 1; ++nr;
            }
        if(!ok) S.pop();
    }
    return nr == n;
}


int esteEulerian()
{
    if(!DFS(1)) return 0;
    for(int i = 1; i <= n; ++i)
        if(deg[i] & 1) return 0;
    return 1;
}


void creareLant(int vf)
{
    int vf2;
    lista *nod, *aux;
    while(deg[vf])
    {
        nod = v[vf];
        S.push(nod->x); vf2 = nod->x;
        //tb sa stergem muchia vf - vf2
        
        //il sterg pe vf2 din lista lui vf
        if(v[vf]->urm == NULL) delete v[vf];
        else
        {
            aux = v[vf]->urm;
            *v[vf] = *v[vf]->urm;
            delete aux;
        }
        
        //il sterg pe vf din lista lui vf2
        if(vf != vf2)
        {
            if(v[vf2]->x == vf)
            {
                if(v[vf2]->urm == NULL) delete v[vf2], v[vf2] = NULL;
                else
                {
                    aux = v[vf2]->urm;
                    *v[vf2] = *v[vf2]->urm;
                    delete aux;
                }
            }
            else
            {
                for(nod = v[vf2]; nod->urm->x != vf; nod = nod->urm) ;
                if(nod->urm->urm == NULL) aux = nod->urm, nod->urm = NULL, delete aux;
                else
                {
                    
                    aux = nod->urm;
                    nod->urm = nod->urm->urm;
                    delete aux;
                }
            }
        }
        --deg[vf]; --deg[vf2];
        vf = vf2;
    }
}


void push1(int a, int b)
{
    lista *nou, *nod;
    nou = new lista; nou->x = b; nou->urm = NULL;
    if(v[a] == NULL) {v[a] = nou; return;}
    for(nod = v[a]; nod->urm != NULL; nod = nod->urm) ;
    nod->urm = nou;
}