Cod sursa(job #3213057)

Utilizator sandry24Grosu Alexandru sandry24 Data 12 martie 2024 13:55:26
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second

vector<vector<pi>> adj(100005); //adj[i].s index of edge
vector<bool> visited_edge(500005); 
vi cycle;

void dfs(int x){
    while(adj[x].size() != 0){
        pi to = adj[x].back();
        adj[x].pop_back();
        if(!visited_edge[to.s]){
            visited_edge[to.s] = 1;
            dfs(to.f);
        }
    }
    cycle.pb(x);
}

void solve(){
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        adj[a].pb({b, i});
        adj[b].pb({a, i});

    }
    for(int i = 1; i <= n; i++){
        if(adj[i].size() % 2){
            cout << -1 << '\n';
            return;
        }
    }
    dfs(1);
    cycle.pop_back();
    for(auto i : cycle)
        cout << i << ' ';
    cout << '\n';
}   

int main(){
    freopen("ciclueuler.in", "r", stdin);
    freopen("ciclueuler.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
}