Cod sursa(job #2032580)

Utilizator tziplea_stefanTiplea Stefan tziplea_stefan Data 5 octombrie 2017 13:41:37
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <fstream>
#include <vector>
#include <cstdio>
#include <stack>
#include <algorithm>
#define VAL 100005

using namespace std;

int N, M, i, j;
int A, B, nod, nr;
int X[VAL];
stack <int> S;
vector <int> G[VAL];
vector <int> :: iterator it;
bool ciclu=true;

int main()
{
    freopen("ciclueuler.in", "r", stdin);
    freopen("ciclueuler.out", "w", stdout);
    scanf("%d %d", &N, &M);
    for (i=1; i<=M; i++)
    {
        scanf("%d %d", &A, &B);
        X[A]++;
        X[B]++;
        G[A].push_back(B);
        G[B].push_back(A);
    }
    for (i=1; i<=N; i++)
    {
        if (X[i] % 2==1)
        {
            ciclu=false;
            break;
        }
    }
    if (ciclu==false)
      printf("-1\n");
    else
    {
        nod=1;
        while (1)
        {
            if (G[nod].empty()==true && S.empty()==true)
              break;
            if (G[nod].empty()==true)
            {
                printf("%d ", nod);
                nod=S.top();
                S.pop();
            }
            else
            {
                S.push(nod);
                nr=G[nod].back();
                G[nod].pop_back();
                G[nr].erase(find(G[nr].begin(), G[nr].end(), nod));
                nod=nr;
            }
        }
    }
    return 0;
}