Cod sursa(job #1561882)

Utilizator lflorin29Florin Laiu lflorin29 Data 4 ianuarie 2016 17:26:47
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <bits/stdc++.h>

using namespace std;

const int N = 100007;

int n, m;
vector<int>edge[N];

int main()
{
    freopen("ciclueuler.in", "r", stdin);
    freopen("ciclueuler.out", "w", stdout);

    scanf("%d %d", &n, &m);
    while(m --) {
         int xx, yy;
         scanf("%d %d", &xx, &yy); xx--; yy--;
         edge[xx].push_back(yy); edge[yy].push_back(xx);
    }

    for(int i = 0; i < n; ++i)
        if(edge[i].size() & 1) {
            printf("%d", -1); return 0;
        }

    stack<int>vertex;
    vertex.push(0);

    vector<int>cycle;
    while(!vertex.empty()) {
         int act = vertex.top();
         if(edge[act].size() == false) {
            cycle.push_back(act); vertex.pop();
         }
         else {
              int toAdd = edge[act].back(); edge[act].pop_back();
              auto itnow = find(begin(edge[toAdd]), end(edge[toAdd]), act);
              edge[toAdd].erase(itnow), vertex.push(toAdd);
         }
    }

    cycle.pop_back();
    for(int values : cycle)
        printf("%d ", values + 1);
}