Cod sursa(job #2902902)

Utilizator tiut_cristianTiut Cristian tiut_cristian Data 16 mai 2022 22:13:05
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.36 kb
#include <bits/stdc++.h>

using namespace std;

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

vector<int> G[100001];
vector<int> ans;

bool folosit[500001];
pair<int, int> muchie[500001];
int n;

void citire()
{
    int m;
    fin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int x, y;
        fin >> x >> y;
        G[x].push_back(i);
        G[y].push_back(i);
        muchie[i].first = x;
        muchie[i].second = y;
    }
}

void afisare()
{
    for (int i = 0; i < ans.size()-1; i++)
        fout << ans[i] << ' ';
}

void euler()
{
    vector<int> st;
    st.push_back(1);
    while (!st.empty())
    {
        int node = st.back();
        if(!G[node].empty())
        {
            int e = G[node].back();
            G[node].pop_back();
            if (!folosit[e])
            {
                folosit[e] = true;
                int rez = muchie[e].first ^ muchie[e].second ^ node;
                st.push_back(rez);
            }
        }
        else
        {
            st.pop_back();
            ans.push_back(node);
        }
    }
}

int main()
{
    citire();
    for (int i = 1; i <= n; i++)
        if (G[i].size() & 1)
        {
            fout << "-1\n";
            return 0;
        }
    euler();
    afisare();

    fin.close();
    fout.close();
}