Cod sursa(job #2806138)

Utilizator namesurname01Name Surname namesurname01 Data 22 noiembrie 2021 13:26:31
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <fstream>
#define M 500002
#define N 100002
#include <vector>

using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");

int n, m, v[M], nr;
bool viz[M];
struct bla
{
    int ve, ord;
};
vector <bla> graph[N];

void euler(int nod)
{
    while (graph[nod].size())
    {
        int vee = graph[nod].back().ve;
        int mu = graph[nod].back().ord;
        graph[nod].pop_back();
        if (!viz[mu])
            viz[mu] = 1, euler(vee);
    }
    v[++nr] = nod;
}
int main()
{
    f >> n >> m;
    int x, y;
    for (int i = 1;i <= m;++i)
    {
        f >> x >> y;
        graph[x].push_back({ y,i });
        graph[y].push_back({ x,i });
    }
    for (int i = 1;i <= n;++i)
        if (graph[i].size() % 2 != 0)
        {
            g << -1 << '\n';
            f.close();
            g.close();
            return 0;
        }
    euler(1);
    for (int i = 1;i < nr;++i) g << v[i] << ' ';
    f.close();
    g.close();
    return 0;
}