Cod sursa(job #2342298)

Utilizator AndreosAndrei Otetea Andreos Data 12 februarie 2019 18:50:03
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
vector<int>G[100005];
int st[500005],stop=0;
inline void Euler(int node)
{
    int other;
    while(!G[node].empty())
    {
        st[++stop]=node;
        other=G[node].back();
        G[node].pop_back();
        G[other].erase(find(G[other].begin(),G[other].end(),node));
        node=other;
    }
}
int main()
{
    freopen("ciclueuler.in","r",stdin);
    freopen("ciclueuler.out","w",stdout);
    int n,x,y,i,m,now;
    bool ok;
    scanf("%d%d",&n,&m);
    for(i=1;i<=m;++i)
    {
        scanf("%d%d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    ok=1;
    for(i=1;i<=n;++i)
    {
        if(G[i].size()&1)
        {
            ok=0;
            break;
        }
    }
    if(ok==0)
        printf("-1\n");
    else
    {
        now=1;
        stop=-1;
        do
        {
            Euler(now);
            now=st[stop];
            printf("%d ",now);
            stop--;
        }
        while(stop!=-1);
    }
    return 0;
}