Cod sursa(job #2845467)

Utilizator vmnechitaNechita Vlad-Mihai vmnechita Data 7 februarie 2022 21:20:22
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pf push_front
#define MOD 1000000007
#define MAX 100005

using namespace std;

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

queue < int > v[MAX];
vector < int > R;
map < pair < int, int >, short > h;

void dfs(int nod);

int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int n, m, x, y, i, nr[MAX];

    fin >> n >> m;
    for(i = 1; i <= n; i++) nr[i] = 0;
    while(m--)
    {
        fin >> x >> y;
        if(x != y) v[x].push(y), v[y].push(x);
        else v[x].push(y), nr[x]++;
    }

    for(i = 1; i <= n; i++) if((v[i].size() - nr[i]) % 2 == 1) i = n + 1;
    if(i == n + 2) fout << -1;
    else
    {
        dfs(1);
        fout << R.size() << '\n';
        for(auto it:R) fout << it << ' ';
    }

    return 0;
}

void dfs(int nod)
{
    int x;

    while(v[nod].empty() == 0)
    {
        x = v[nod].front(), v[nod].pop();
        if(x != nod)
        {
            h[{nod, x}]++, h[{x, nod}]--;
            if(h[{nod, x}] > 0) dfs(x);
        }
        else dfs(x);
    }
    R.pb(nod);
}