Cod sursa(job #2422853)

Utilizator TudorCristian2Lechintan Tudor TudorCristian2 Data 20 mai 2019 08:48:37
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <fstream>
#include <vector>
#include <bitset>

const int MaxN = 100001;
const int MaxE = 500001;

struct Edge
{
    int x, y;
};

using VI  = std::vector<int>;
using VE  = std::vector<Edge>;
using VVI = std::vector<VI>;

std::ofstream fout("graf.out");
VVI G;
VE E;
VI ce;
std::bitset<MaxE> v;
int n, m;

void Read();
bool Euler();
void Dfs(int x);
void Write();

int main()
{
    Read();

    if (!Euler())
        fout << -1;
    else
    {
        Dfs(1);
        Write();
    }

    return 0;
}

void Read()
{
    std::ifstream fin("graf.in");
    fin >> n >> m;

    G = VVI(n + 1);
    E = VE(m + 1);

    int x = 0, y = 0;

    for (int i = 1; i <= m; ++i)
    {
        fin >> x >> y;
        G[x].push_back(i);
        G[y].push_back(i);
        E[i] = { x, y };
    }
}

bool Euler()
{
    for (int i = 1; i <= n; ++i)
    {
        if (G[i].size() % 2 == 1) return false;
    }

    return true;
}

void Dfs(int x)
{
    for (int e : G[x])
    {
        if (v[e]) continue;
        v[e] = 1;

        Dfs((E[e].x == x) ? E[e].y : E[e].x);
    }

    ce.push_back(x);
}

void Write()
{
    for (size_t i = 0; i < ce.size() - 1; ++i)
    {
        fout << ce[i] << ' ';
    }
}