Cod sursa(job #1649846)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 11 martie 2016 15:22:05
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <cstring>
#include <bitset>
#define INF 0x3f3f3f3f
#define edge g[nod][g[nod].size() - 1]
using namespace std;

ifstream is("ciclueuler.in");
ofstream os("ciclueuler.out");

using VI = vector<int>;
using VVI = vector<VI>;

int n, m;
VI answ;
VVI g;
vector<pair<int, int>> e;
bitset<500001> ok;

void Read();
bool Check();
inline void Solve(int nod);

int main()
{
    Read();
    if ( !Check() )
        os << "-1";
    else
        Solve(1);
    is.close();
    os.close();
    return 0;
}

inline void Solve(int nod)
{
    while ( g[nod].size() )
        if ( ok[edge] )
            g[nod].pop_back();
        else
        {
            ok[edge] = true;
            Solve(e[edge].first + e[edge].second - nod);
        }
    if ( answ.size() )
    {
        os << answ[0] << " ";
        answ.pop_back();
    }
    answ.push_back(nod);
}

bool Check()
{
    if ( m < n - 1 )
        return false;
    for ( int x = 1; x <= n; ++x )
        if ( !g[x].size() || g[x].size() & 1 )
            return false;
    return true;
}

void Read()
{
    is >> n >> m;
    g = VVI(n + 1);
    int x, y;
    for ( int i = 0; i < m; ++i )
    {
        is >> x >> y;
        g[x].push_back(i);
        g[y].push_back(i);
        e.push_back(make_pair(x, y));
    }
}