Cod sursa(job #2290438)

Utilizator DordeDorde Matei Dorde Data 26 noiembrie 2018 15:24:31
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.11 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <algorithm>
#define pb push_back
using namespace std;
ofstream g ("ciclueuler.out");
struct code {
    int x , y;
};
int const NM = 1e5 , BUFF = 1e6;
char buff [BUFF];
int p;
vector <int> v2 [1 + NM];
vector <int> e;
bool viz [1 + 5 * NM];
code v [1 + 5 * NM];
void out (vector <int> af){
    for(auto i : af)
        g << i << ' ';
}
void next (){
    if (++ p == BUFF){
        p = 0;
        fread (buff , 1 , BUFF , stdin);
    }
}
void euler (int node){
    for(auto i : v2 [node]){
        if (! viz [i]){
            int to = v [i] . x;
            if (to == node)
                to = v [i] . y;
            viz [i] = true;
            euler (to);
        }
    }
    e . pb (node);
}
int nextc (){
    while (! isdigit (buff [p]))
        next ();
    int a = 0;
    while (isdigit (buff [p])){
        a = a * 10 + buff [p] - '0';
        next ();
    }
    return a;
}
void nu (){
    g << -1;
    return;
}
int n;
void flood (int node){
    viz [node] = true;
    for(auto i : v2 [node]){
        int next = v [i] . x;
        if (v [i] . x == node){
            next = v [i] . y;
        }
        if (! viz [next])
            flood (next);
    }
}
bool check (){
    flood (1);
    for(int i = 2 ; i <= n ; ++ i)
        if (! viz [i])
            return true;
    for(int i = 1; i <= n ; ++ i)
        if (v2 [i] . size () % 2)
            return true;
    return false;
}
int main()
{
    int m;
    freopen ("ciclueuler.in" , "r" , stdin);
    fread (buff , 1 , BUFF , stdin);
    n = nextc ();
    m = nextc ();
    for(int i = 1 ; i <= m ; ++ i){
        int a , b;
        a = nextc ();
        b = nextc ();
        v2 [a] . pb (i);
        v2 [b] . pb (i);
        v [i] = {a , b};
    }
    if (check ()){
        nu ();
        return 0;
    }
    fill (viz + 1 , viz + m + 1 , false);
    for(int i = 1 ; i <= n ; ++ i)
        if (! viz [i]){
            viz [i] = true;
            euler (i);
            out (e);
            return 0;
        }
    return 0;
}