Cod sursa(job #1022879)

Utilizator a96tudorAvram Tudor a96tudor Data 6 noiembrie 2013 08:21:57
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include<cstdio>
#include<vector>
#include<list>

#define pf push_front
#define pb push_back
#define N_MAX 100010
#define M_MAX 500010
using namespace std;

vector <int> G[N_MAX];
list <int> Q;

inline void Euler(int x)
{
    int y;
    vector <int> :: iterator it;
    Q.pb(x);
    while (!Q.empty())
    {
        x=Q.front();
        if (G[x].empty())
            {
                Q.pop_front();
                printf("%d ",x);
            }
            else
                {
                    y=G[x].back();
                    G[x].pop_back();
                    Q.pf(y);
                    for (it=G[x].begin();it!=G[x].end();++it)
                        if (*it==y)
                            {
                                G[x].erase(it);
                                break;
                            }
                }
    }
}
inline void Read_Data()
{
    int N,M,x,y,i;
    bool ok=true;
    scanf("%d%d",&N,&M);
    for (i=1;i<=M;i++)
    {
        scanf("%d%d",&x,&y);
        G[x].pb(y);
        G[y].pb(x);
    }
    for (i=1;i<=N;i++)
        if (G[i].size()%2==1) ok=false;
    if (ok) Euler(1);
        else printf("-1");
    printf("\n");
}
int main()
{
    freopen("ciceuler.in","r",stdin);
    freopen("ciceuler.out","w",stdout);
    Read_Data();
    fclose(stdin);
    fclose(stdout);
    return 0;
}