Cod sursa(job #2367095)

Utilizator Alex03Runcan Alexandru Alex03 Data 5 martie 2019 08:31:48
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <bits/stdc++.h>
#define SZ(x) ((int) (x).size())
using namespace std;
ifstream fin("ciclueuler.in"); ofstream fout("ciclueuler.out"); 
const int NMAX = 100005, MMAX = 500005;
vector<int> G[NMAX];
 
bool usedEdge[MMAX];
int from[MMAX], to[MMAX];
 
int main() 
{ 
    int n, m;
    fin >> n >> m;
 
    for (int i = 0; i < m; ++i) 
    {
        int x, y;
        fin >> x >> y;
        G[x].push_back(i);
        G[y].push_back(i);
        from[i] = x;
        to[i] = y;
    }
 
    for (int i = 1; i <= n; ++i) 
    {
        if (SZ(G[i]) & 1) 
        {
            fout << "-1\n";
            return 0;
        }
    }
 
    vector<int> ans;
 
    vector<int> stk;
    stk.push_back(1);
    while (!stk.empty()) 
    {
        int node = stk.back();
        if (!G[node].empty()) 
        {
            int e = G[node].back();
            G[node].pop_back();
            if (!usedEdge[e]) 
            {
                usedEdge[e] = true;
                int to = ::from[e] ^ ::to[e] ^ node;
                stk.push_back(to);
            }
        } else 
        {
            stk.pop_back();
            ans.push_back(node);
        }
    }
 
    for (int i = 0; i < SZ(ans) - 1; ++i) 
    {
        fout << ans[i] << ' ';
    }
    fout << '\n';
 
    fin.close();
    fout.close();
}