Cod sursa(job #2570108)

Utilizator XXMihaiXX969Gherghinescu Mihai Andrei XXMihaiXX969 Data 4 martie 2020 15:08:54
Problema Ciclu Eulerian Scor 50
Compilator cpp-64 Status done
Runda r3capitusulare Marime 1.05 kb
#include <bits/stdc++.h>

using namespace std;

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

const int DIM = 1e5 + 7;

vector < pair <int,int> > l[DIM];

vector <int> rez;

int nr[DIM];
bool viz[DIM];

void dfs(int x)
{
    while(!l[x].empty())
    {
        int nod = l[x].back().first;
        int poz = l[x].back().second;
        l[x].pop_back();

        if(viz[poz] == false)
        {
            viz[poz] = true;
            dfs(nod);
        }

    }

    rez.push_back(x);
}

int main()
{
    int n, m;
    in >> n >> m;


    for(int i = 1; i <= m ; i++)
    {
        int x, y;
        in >> x >> y;

        l[x].push_back({y,i});
        l[y].push_back({x,i});

        nr[x]++;
        nr[y]++;
    }

    for(int i = 1; i <= n; i++)
        if(nr[i] % 2)
        {
            out << -1;
            return 0;
        }

    dfs(1);

    rez.pop_back();

    for(int i = 0 ; i < rez.size(); i++)
        out << rez[i] <<" ";

    in.close();
    out.close();
    return 0;
}