Cod sursa(job #2823479)

Utilizator francescom_481francesco martinut francescom_481 Data 28 decembrie 2021 17:23:07
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <bits/stdc++.h>
#define DAU ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define PLEC return 0;

using namespace std;

ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
#define cin fin
#define cout fout

#define N 500005
vector < vector < pair < int , int > > > g;
int v[N], f[N], st, dr, i, x, y, k, n, m, ind;

void fa(int nod)
{
    for(auto t : g[nod])
    {
        if(f[t.second] == 0)
        {
            f[t.second] = 1;
            fa(t.first);
        }
    }
    v[++st] = nod;
}

int main()
{
    DAU
    cin >> n >> m;
    g.resize(n+5);
    for(int i = 1 ; i <= m ; i++)
    {
        cin >> x >> y;
        g[x].push_back({y,i});
        g[y].push_back({x,i});
    }
    for(int i = 1 ; i <= n ; i++)
    {
        if(g[i].size() % 2 == 1)
        {
            cout << -1;
            PLEC
        }
    }
    deque < int > c;
    c.push_front(1);
    while(!c.empty())
    {
        int nod = c.front();
        if(g[nod].size() != 0)
        {
            int x = g[nod][g[nod].size()-1].second;
            if(f[x] == 0)
            {
                f[x] = 1;
                c.push_front(g[nod][g[nod].size()-1].first);
            }
            g[nod].pop_back();
        }
        else
        {
            v[++st] = nod;
            c.pop_front();
        }
    }
    for(int i = 1 ; i <= st ; i++)cout << v[i] << " ";
    PLEC
}