Cod sursa(job #2870695)

Utilizator 100pCiornei Stefan 100p Data 12 martie 2022 15:10:04
Problema Ciclu Eulerian Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <bits/stdc++.h>
#define FILES freopen("ciclueuler.in","r",stdin);\
              freopen("ciclueuler.out","w",stdout);
#define ll long long
#define fastio ios_base::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
#define MAX 100000
#define pb push_back
#define mp make_pair
#define mod 666013
using namespace std;
vector<int>ans;
bool check[MAX+5];
int n,m,a,b,grad[MAX+5];
vector<pair<int,int>> v[MAX+5];
void dfs(int x)
{
    while(!v[x].empty())
    {
        int r = v[x].back().first,  y = v[x].back().second;
        v[x].pop_back();
        if(!check[y])
        {
            check[y] = 1;
            dfs(r);
        }
    }
    ans.pb(x);
}
int main()
{
    fastio
    FILES
    cin >> n >> m;
    for(int i = 1; i <= m; ++i)
    {
        cin >> a >> b;
        v[a].pb(mp(b,i)),v[b].pb(mp(a,i));
        grad[a]++,grad[b]++;
    }
    for(int i = 1; i <= n; ++i)
    {
        if(grad[i]%2)
        {
            cout << "-1\n";
            exit(0);
        }
    }
    dfs(1);
    reverse(ans.begin(),ans.end());
    ans.pop_back();
    for(int i = 0;i < ans.size(); ++i) cout << ans[i] << ' ';
}