Cod sursa(job #2342259)

Utilizator AndreosAndrei Otetea Andreos Data 12 februarie 2019 18:26:15
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
vector<int>G[100005];
vector<int>::iterator it;
stack<int>st;
void Euler(int node)
{
    int other;
    while(!G[node].empty())
    {
        st.push(node);
        other=G[node].back();
        G[node].pop_back();
        it=find(G[other].begin(),G[other].end(),node);
        G[other].erase(it);
        node=other;
    }
}
int main()
{
    int n,x,y,i,m,now;
    bool ok;
    fin>>n>>m;
    for(i=1;i<=m;++i)
    {
        fin>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    ok=1;
    for(i=1;i<=n;++i)
    {
        if(G[i].empty() || G[i].size()%2==1)
        {
            ok=0;
            break;
        }
    }
    if(ok==0)
        fout<<-1<<"\n";
    else
    {
        now=1;
        do
        {
            Euler(now);
            now=st.top();
            fout<<now<<" ";
            st.pop();
        }
        while(!st.empty());
    }
    return 0;
}