Cod sursa(job #1776613)

Utilizator GeanaVladGeana Vlad GeanaVlad Data 11 octombrie 2016 17:33:16
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include <iostream>
#include<fstream>
#include<vector>
#define Nmax 100001
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
vector<int>v[Nmax];
int n,m,i,x,y,k,st[550000];
bool ok=1;
inline void sterge(int x,int y)
{
    int l=v[x].size();
    bool ok=1;
    for(int k=0; k<l && ok; k++)
        if(v[x][k]==y)
        {
            swap(v[x][k],v[x][l-1]);
            v[x].pop_back();
            ok=0;
        }
}
inline void euler()
{
    int nod,nod2;
    while(k>0)
    {
        nod=st[k];
        if(v[nod].size())
        {
            nod2=v[nod][0];
            sterge(nod,nod2);
            sterge(nod2,nod);
            st[++k]=nod2;
        }
        else
        {
            k--;
            if(k)
                g<<st[k]<<' ';
        }
    }
}
int main()
{
    f>>n>>m;
    for(i=1; i<=m; i++)
    {
        f>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(i=1; i<=n; i++)
        if(v[i].size()%2==1)
        {
            ok=0;
            cout<<-1;
            break;
        }
    k=1;
    st[k]=1;
    if(ok)
        euler();
}