Cod sursa(job #2252858)

Utilizator MarianConstantinMarian Constantin MarianConstantin Data 3 octombrie 2018 10:38:49
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define N 100010
#define M 500010

using namespace std;

vector<int> edges[N];
vector<int> answer;
stack<int> st;
int e1[M], e2[M];
bool usedEdge[M];

int main()
{
    ifstream fin("ciclueuler.in");
    ofstream fout("ciclueuler.out");
    int n, m, x, y, node, edge, to;
    fin >> n >> m;
    for (int i=0; i<m; i++)
    {
        fin >> x >> y;
        edges[x].push_back(i);
        edges[y].push_back(i);
        e1[i]=x;
        e2[i]=y;
    }
    for (int i=1; i<=n; i++)
    {
        int currentSize=edges[i].size();
        if (currentSize%2==1)
        {
            fout << -1;
            return 0;
        }
    }
    st.push(1);
    while(!st.empty())
    {
        node=st.top();
        if (edges[node].size())
        {
            edge=edges[node].back();
            edges[node].pop_back();
            if (!usedEdge[edge])
            {
                if (e1[edge]==node)
                    to=e2[edge];
                else
                    to=e1[edge];
                st.push(to);
                usedEdge[edge]=1;
            }
        }
        else
        {
            st.pop();
            answer.push_back(node);
        }
    }
    for (int i=0; i<answer.size(); i++)
        fout << answer[i] << " ";
    return 0;
}