Cod sursa(job #3122789)

Utilizator Ion.AAlexandru Ion Ion.A Data 20 aprilie 2023 17:58:29
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int N = 1e5 + 1;
const int M = 5e5 + 1;
int n, m, prim[N];
vector <int> g[N];
vector <int> rez;
bool sterse[M];

struct muchie{
    int x, y;
};
muchie e[M];

int celalalt_vf(muchie e, int x)
{
    return (e.x + e.y - x);
}

void euler(int x)
{
    while (prim[x] < (int)g[x].size())
    {
        int i_e_c = g[x][prim[x]++];
        muchie e_c = e[i_e_c];
        int y = celalalt_vf(e_c, x);
        if (!sterse[i_e_c])
        {
            sterse[i_e_c] = true;
            euler(y);
        }
    }
    rez.push_back(x);
}

int main()
{
    in >> n >> m;
    for (int i = 0; i < m; i++)
    {
        in >> e[i].x >> e[i].y;
        g[e[i].x].push_back(i);
        g[e[i].y].push_back(i);
    }
    ///verific daca toate nodurile au grade pare
    for (int i = 1; i <= n; i++)
    {
        if (g[i].size() % 2 != 0)
        {
            out << -1;
            return 0;
        }
    }
    euler(1);
    if ((int)rez.size() != m + 1)
    {
        out << -1;
        return 0;
    }
    for (int i = 0; i < (int)rez.size() - 1; i++)
    {
        out << rez[i] << " ";
    }
    return 0;
}