Cod sursa(job #2836664)

Utilizator TiberiwTiberiu Amarie Tiberiw Data 20 ianuarie 2022 18:35:34
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <bits/stdc++.h>
#define Dmax 100005

using namespace std;

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


int n,m;

vector < pair <int,int> > M;
vector < bool > elim;
vector < int > G[Dmax], L;

int main()
{

    f>>n>>m;
    int x,y;
    while(f>>x>>y)
    {
        M.push_back(make_pair(x,y));
        elim.push_back(false);
        G[x].push_back(M.size()-1);
        G[y].push_back(M.size()-1);
    }
    for(int i = 1; i <= n; i++)
        if(G[i].size() & 1)
    {
        cout<<-1;
        return 0;
    }


    stack <int> S;
    S.push(1);
    while(!S.empty())
    {
        int k = S.top();
        if(!G[k].empty())
        {
            int x = G[k].back();
            G[k].pop_back();
            if(!elim[x])
            {
                elim[x] = true;
                int p = M[x].second;
              //  if(p==k)
                   // p = M[x].first;
                S.push(p);
            }
        }
        else
        {
            L.push_back(k);
            S.pop();
        }

    }

for(unsigned int i = 0 ; i<L.size(); i++)
    g<<L[i]<<" ";

    return 0;
}