Cod sursa(job #2323232)

Utilizator tnocNume corect tnoc Data 18 ianuarie 2019 23:02:28
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <iostream>
#include <cstdio>
#include <list>
#include <algorithm>
#define NMAX 100010
#define MMAX 500010

using namespace std;

int n, m, sterse;
list<int> v[NMAX];
int stiv[MMAX], st;

void citire()
{
    scanf("%d %d", &n, &m);
    int t1, t2;
    for (int i = 1; i <= m; i++)
    {
        scanf("%d %d", &t1, &t2);
        v[t1].push_back(t2);
        v[t2].push_back(t1);
    }
}

void euler()
{
    for (int i = 1; i <= n; i++)
        if (v[i].size()%2)
    {
         printf("-1");
            return;
    }
    stiv[++st] = 1;
    while (st > 0)
    {
        int top = stiv[st];

        if (v[top].empty())
        {
            printf("%d ", top);
            st--;
        }
        else {
            int next = v[top].front();
            v[top].pop_front();
            v[next].erase(std::find(v[next].begin(), v[next].end(), top));
            stiv[++st] = next;
        }
    }
}

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

    citire();
    euler();

    return 0;
}