Cod sursa(job #2887125)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 8 aprilie 2022 21:17:11
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <iomanip>
#include <algorithm>

using namespace std;

const string filename = "euler";
ifstream fin(filename + ".in");
ofstream fout(filename + ".out");

const int maxN = 100005;
int n, m;
bool used[5 * maxN];
struct muchie {
    int nod, ind;
};
vector <muchie> G[maxN];
vector <int> stiva;

int main()
{
    fin >> n >> m;
    for(int x, y, i = 1; i <= m; i++)
    {
        fin >> x >> y;
        G[x].push_back({y, i});
        G[y].push_back({x, i});
    }
    bool ok = 1;
    for(int i = 1; i <= n; i++)
        if(G[i].size() % 2 == 1)
            ok = 0;
    if(!ok)
    {
        fout << -1;
        return 0;
    }
    stiva.push_back(1);
    for(int i = 1; i <= m; i++)
    {
        int nod = stiva.back();
        while(!G[nod].empty())
        {
            muchie nxt = G[nod].back();
            G[nod].pop_back();
            if(used[nxt.ind])
                continue;
            used[nxt.ind] = 1;
            nod = nxt.nod;
            stiva.push_back(nod);
        }
        fout << nod << ' ';
        stiva.pop_back();
    }
    return 0;
}