Cod sursa(job #2663368)

Utilizator razviii237Uzum Razvan razviii237 Data 26 octombrie 2020 10:54:38
Problema Ciclu Eulerian Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.46 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");

struct xy { int x, y; };
const int maxn = 1e5+5, maxm = 5e5+5;

int n, m, x, y, rez;
int fr[maxn], done[maxm];

vector <xy> nod[maxn];

/*void euler(int x) {
    if(rez >= m) { return; }
    for(auto u : nod[x]) {
        if(done[u.y] == 0) {
            done[u.y] = 1;
            euler(u.x);
        }
    }
    if(rez < m) {
        g << x << ' '; rez ++;
    }
}*/

stack <xy> st;
stack <int> strez;
void euler2(int frst) {
    st.push({frst, 0});
    int x, y;
    while(st.empty() == false) {
        x = st.top().x; y = st.top().y;
        st.pop();
        if(done[y] == true) { continue; }
        done[y] = true;
        for(auto u : nod[x]) {
            if(done[u.y] == false) {
                //done[u.y] = true;
                st.push({u});
            }
        }
        strez.push(x);
    }
    for(int i = 1; i <= m; i ++) {
        g << strez.top() << ' '; strez.pop();
    }
}

int main()
{
    int i;
    f >> n >> m;
    for(i = 1; i <= m; i ++) {
        f >> x >> y;
        nod[x].push_back({y, i});
        nod[y].push_back({x, i});
        fr[x] ++; fr[y] ++;
    }
    for(i = 1; i <= n; i ++) {
        if(fr[i] % 2 == 1) {
            g << -1; return 0;
        }
    }

    euler2(1);

    f.close(); g.close();
    return 0;
}