Cod sursa(job #2845500)

Utilizator vmnechitaNechita Vlad-Mihai vmnechita Data 7 februarie 2022 22:01:18
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 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 < pair < int, int > > v[MAX];
stack < int > s;
bitset < 5 * MAX > viz;
int nr[MAX];

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

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

    for(i = 1; i <= n; i++) if(v[i].size() % 2 == 1) i = n + 1;
    if(i == n + 2) fout << -1;
    else
    {
        s.push(1);
        while(s.empty() == 0)
        {
            nod = s.top();
            if(v[nod].empty() == 0)
            {
                if(viz[v[nod].front().second] == 0)
                {
                    x = v[nod].front().first, viz[v[nod].front().second] = 1, v[nod].pop();
                    s.push(x);
                }
                else v[nod].pop();
            }
            else
            {
                fout << nod << ' ';
                while(nr[nod] != 0) fout << nod << ' ', nr[nod]--;
                s.pop();
            }
        }
    }

    return 0;
}