Cod sursa(job #1776631)

Utilizator GeanaVladGeana Vlad GeanaVlad Data 11 octombrie 2016 17:49:54
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <iostream>
#include<fstream>
#include<cstdio>
#include<vector>
#define Nmax 100010
using namespace std;
vector<int>v[Nmax];
long long n,m,i,x,y,k,st[500011];
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)
                printf("%d ",st[k]);
        }
    }
}
int main()
{
    freopen("ciclueuler.in","r",stdin);
    freopen("ciclueuler.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(i=1; i<=m; i++)
    {
        scanf("%d%d",&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();
}