Cod sursa(job #2693010)

Utilizator TheGodFather2131Alexandru Miclea TheGodFather2131 Data 4 ianuarie 2021 15:50:43
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.77 kb
//ALEXANDRU MICLEA

#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
#include <chrono>
#include <assert.h>

using namespace std;
using ll = long long;

#include <fstream>
ifstream cin("input.in"); ofstream cout("output.out");
//ifstream cin("ciclueuler.in"); ofstream cout("ciclueuler.out");

//VARIABLES

const int MAXN = 1e5 + 5;
const int MAXM = 5e5 + 5;

vector <int> gr[MAXN];
int pos[MAXN];

struct edge {
    int st; int dr;
    bool used;

} edges[MAXM];

vector <int> ans;

//FUNCTIONS

int next(int nod, edge& mch) {
    mch.used = true;
    if (mch.st == nod) return mch.dr;
    else return mch.st;
}

void dfs(int nod) {
    while (pos[nod] + 1 < (int)gr[nod].size()) {
        pos[nod]++;
        if (!edges[gr[nod][pos[nod]]].used) {
            dfs(next(nod, edges[gr[nod][pos[nod]]]));
        }
    }
    ans.push_back(nod);
}

//MAIN

int main() {

    int n, m; cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        pos[i] = -1;
    }

    for (int i = 1; i <= m; i++) {
        int a, b; cin >> a >> b;
        edges[i] = { a, b, false };
        gr[a].push_back(i);
        gr[b].push_back(i);
    }

    for (int i = 1; i <= n; i++) {
        if (gr[i].size() % 2) {
            cout << "-1\n";
            return 0;
        }
    }

    dfs(1);

    ans.pop_back();

    if (ans.size() != m) {
        cout << "-1\n";
        return 0;
    }

    for (auto& x : ans) cout << x << ' ';

    return 0;
}