Cod sursa(job #2695139)

Utilizator AndreiAlexandru2k3Ciucan Andrei Alexandru AndreiAlexandru2k3 Data 11 ianuarie 2021 21:56:09
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>
using namespace std;

ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");

const int N_MAX = 100001;

int N, M, grad[N_MAX];

list<int>G[N_MAX];
stack<int>S;
vector<int>sol;

void Euler()
{
    S.push(1);
    int x, y;
    while(!S.empty())
    {
        x = S.top();
        while(!G[x].empty())
        {
            y = G[x].front();
            G[x].pop_front();
            G[y].erase(find(G[y].begin(), G[y].end(), x));
            S.push(y);
            x=y;
        }
        sol.push_back(S.top());
        S.pop();
    }
}

int main()
{
    f >> N >> M;
    while(M--)
    {
        int x, y;
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
        grad[x]++;
        grad[y]++;
    }
    for(int i = 1; i <= N; i++)
    {
        if(grad[i] & 1 )
        {
            g << "-1\n";
            return 0;
        }
    }
    Euler();
    for(auto it:sol)
        g<<it<<' ';
    return 0;
}