Cod sursa(job #3341859)

Utilizator Cristian_NegoitaCristian Negoita Cristian_Negoita Data 21 februarie 2026 13:19:27
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int NMAX = 1e5 + 1;
int n, m;
bool viz[5 * NMAX];
vector<pair<int, int>> adj[NMAX];
vector<int> ciclu;

bool grad_impar()
{
    bool check = false;
    for(int node = 1; node <= n; node++)
        check |= (adj[node].size() & 1);
    return check;
}

bool all_used()
{
    bool check = true;
    for(int edge = 1; edge <= m; edge++)
        check &= viz[edge];
    return check;
}

void dfs(int node)
{
    for(auto [next, id] : adj[node])
    {
        if(viz[id])
            continue;
        viz[id] = true;
        dfs(next);
    }
    ciclu.push_back(node);
}

int main()
{
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        int u, v;
        fin >> u >> v;
        adj[u].emplace_back(v, i);
        adj[v].emplace_back(u, i);
    }
    if(grad_impar())
    {
        fout << -1;
        return 0;
    }
    dfs(1);
    if(all_used())
    {
        ciclu.pop_back();
        for(int node : ciclu)
            fout << node << " ";
    }
    else
        fout << -1;

    fin.close();
    fout.close();
    return 0;
}