Cod sursa(job #2822849)

Utilizator francescom_481francesco martinut francescom_481 Data 25 decembrie 2021 22:56:42
Problema Ciclu Eulerian Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 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 4000005
vector < vector < int > > g;
int v[N], st, dr, i, x, y, k, n, m, ind;

void fa(int x)
{
    for(int i = 0 ; i < g[x].size() ;)
    {
        int t = g[x][i];
        for(int j = 0 ; j < g[t].size() ; j++)
        {
            if(g[t][j] == x)
            {
                g[t].erase(g[t].begin()+j);
                break;
            }
        }
        g[x].erase(g[x].begin()+i);
        fa(t);
    }
    v[++st] = x;
}

int main()
{
    DAU
    cin >> n >> m;
    g.resize(n+5);
    for( ; m ; m--)
    {
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    fa(1);
    if(v[1] == 1)
    {
        for(int i = st ; i > 1 ; i--)cout << v[i] << " ";
    }
    else cout << -1;
    PLEC
}