Cod sursa(job #373651)

Utilizator cristikIvan Cristian cristik Data 14 decembrie 2009 17:38:16
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <stdio.h>
#define max 100010
struct lista
{
    int muchie;
    int nod;
    lista *next;
};
lista *g[max];
int n,m,i,j,k,top,stack[max*5],deg[max];
char s[5*max];
void dfs(int nod)//dfs pe muchii
{
    int w;
    stack[++top]=nod;
    while(top)
    {
        w=stack[top]; //printf("%d ",w);
        if(g[w]!=NULL)
         if(!s[g[w]->muchie])
         {
             s[g[w]->muchie]=1;
             stack[++top]=g[w]->nod;
             g[w]=g[w]->next;
         } else g[w]=g[w]->next;
        else printf("%d ",stack[top--]);
    }
}
void push(int i,int j,int k)
{
    lista *p=new lista;
    p->muchie=k;
    p->nod=j;
    p->next=g[i];
    g[i]=p;
}
int main()
{
    freopen("ciclueuler.in","r",stdin);
    freopen("ciclueuler.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(k=1; k<=m; k++)
    {
        scanf("%d%d",&i,&j);
        push(i,j,k);
        push(j,i,k);
        deg[i]++; deg[j]++;
    }
    for(i=1; i<=n; i++)
     if(deg[i]%2==1) { printf("-1"); return 0; }
    dfs(1);
    return 0;
}