Cod sursa(job #2567998)

Utilizator NotTheBatmanBruce Wayne NotTheBatman Data 3 martie 2020 20:06:43
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <stack>

using namespace std;

const int N = 1e5 + 5;

vector <pair<int, int> > L[N];
bitset <5 * N> used;
vector <int> eulcycle;
stack <int> st;
int n;

void Read ()
{
    ifstream fin ("ciclueuler.in");
    int m;
    fin >> n >> m;
    for (int i = 1, x, y; i <= m; i++)
    {
        fin >> x >> y;
        L[x].push_back({y, i});
        L[y].push_back({x, i});
    }
    fin.close();
}

void Solve ()
{
    ofstream fout ("ciclueuler.out");
    for (int i = 1; i <= n; i++)
        if (L[i].size() & 1)
        {
            fout << "-1\n";
            fout.close();
            return;
        }
    st.push(1);
    while (!st.empty())
    {
        int currvertex = st.top();
        if (L[currvertex].size())
        {
            pair<int, int> next = L[currvertex].back();
            L[currvertex].pop_back();
            if (!used[next.second])
            {
                used[next.second] = 1;
                st.push(next.first);
            }
        }
        else
        {
            eulcycle.push_back(currvertex);
            st.pop();
        }
    }
    eulcycle.pop_back();
    for (auto it : eulcycle)
        fout << it << " ";
    fout << "\n";
    fout.close();
}

int main()
{
    Read();
    Solve();
    return 0;
}