Cod sursa(job #1861615)

Utilizator Matei_IgnutaMatei Ignuta Matei_Ignuta Data 29 ianuarie 2017 09:24:52
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>

using namespace std;

stack <int> st;
list <int> v[100010], ans;
void euler(int node)
{
    st.push(node);

    while (!st.empty())
    {
        int vf = st.top();
        while (v[vf].size())
        {
            int next = v[vf].front();
            v[vf].pop_front();
            v[next].erase(find(v[next].begin(), v[next].end(), vf));
            st.push(next);
            vf = next;
        }
        ans.push_back(st.top());
        st.pop();
    }
}
int n, m, x, y;
int main()
{
    freopen("ciclueuler.in", "r", stdin);
    freopen("ciclueuler.out", "w", stdout);
    scanf("%d%d", &n, &m);
    for(int i=1; i<=m; i++)
    {
        scanf("%d%d", &x, &y);
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(int i=1; i<=n; i++)
        {
            if(v[i].size()==0 or v[i].size()%2==1)
            {
                printf("-1");
                return 0;
            }
        }
    euler(1);
    ans.pop_back();
    list<int>::const_iterator i;
    for(i = ans.begin(); i != ans.end(); ++i)
		printf("%d ", *i);
    return 0;
}