Cod sursa(job #2162755)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 12 martie 2018 13:33:08
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>
#define Nmax 100001
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
list <int> G[Nmax];
stack <int> st;
list <int> ans;
int n,m;
void Euler()
{
    list <int> :: iterator it;
    int w,x;
    st.push(1);
    while(!st.empty())
    {
        x=st.top();
        if(!G[x].empty())
        {
            w=*G[x].begin();
            G[x].pop_front();
            for(it=G[w].begin();*it!=x;++it);
            G[w].erase(it);
            st.push(w);
        }
        else
        {
            ans.push_back(x);
            st.pop();
        }
    }
}
int main()
{
    int x,y,i;
    f>>n>>m;
    while(m--)
    {
        f>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for(i=1;i<=n;i++)
        if(((int)G[i].size())&1)
        {
            g<<-1;
            return 0;
        }
    Euler();
    for(list <int> :: iterator it=ans.begin();it!=--ans.end();++it)
        g<<*it<<' ';

    return 0;
}