Cod sursa(job #2072966)

Utilizator dragomirmanuelDragomir Manuel dragomirmanuel Data 22 noiembrie 2017 15:35:27
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include <cstdio>
#include <vector>
#include <stack>

using namespace std;

const int NMax = 100002;
const int MMax = 500002;

struct muchie
{
    int nod1,nod2;
    bool sters = false;

} v[MMax];

int N, M ;
vector <int> G[NMax];
stack <int> ST;

void Read()
{
    scanf("%d%d", &N, &M);

    for(int i=1; i<=M; ++i)
    {
        scanf("%d %d", &v[i].nod1, &v[i].nod2);
        G[v[i].nod1].push_back(i);
        G[v[i].nod2].push_back(i);
    }
}

bool Grade_pare()
{
    for(int i=1; i<=N; ++i)
        if(G[i].size() % 2 == 1)
            return true;

    return false;
}

void Euler()
{
   ST.push(1);

   while(!ST.empty())
   {
       int nod = ST.top();

       if(G[nod].size())
       {
           int next = G[nod].back();
           G[nod].pop_back();

           if(v[next].sters == true)
            continue;

           v[next].sters = true;

           if(v[next].nod1 == nod)
             ST.push(v[next].nod2);

           else
            ST.push(v[next].nod1);

       }

       else
       {
           printf("%d ", ST.top());
           ST.pop();
       }
   }
}

int main()
{
    freopen("ciclueuler.in", "r", stdin);
    freopen("ciclueuler.out", "w", stdout);

    Read();

    if(Grade_pare())
    {
        printf("-1\n");

        return 0;
    }

    Euler();

    return 0;
}