Cod sursa(job #1650945)

Utilizator Mr.DoomRaul Ignatus Mr.Doom Data 11 martie 2016 22:02:02
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;

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

bool IsEuler();
inline void Df(int node);
void Read();

vector<vector<int> > G;
vector<int> c;
vector<pair<int, int> > e;
bitset<500001> v;
int n, m;

int main()
{
    Read();
    if ( IsEuler() )
    {
        Df(1);
        for ( const auto& a : c )
            os << a << ' ';
    }
    else
        os << -1;

    is.close();
    os.close();
    return 0;
}

bool IsEuler()
{
    for ( const auto& g : G )
        if ( g.size() & 1 )
            return false;
    return true;
}

inline void Df(int node)
{
    for ( const auto& g : G[node] )
        if ( !v[g] )
        {
            v[g] = 1;
            Df(e[g].first + e[g].second - node);
        }
    c.push_back(node);
}

void Read()
{
    is >> n >> m;
    G = vector<vector<int> >(n + 1);
    e = vector<pair<int, int> >(m + 1);
    for ( int i = 1, x, y; i <= m; ++i )
    {
        is >> x >> y;
        G[x].push_back(i);
        G[y].push_back(i);
        e[i] = {x, y};
    }
}