Cod sursa(job #383114)

Utilizator cristikIvan Cristian cristik Data 15 ianuarie 2010 18:18:30
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 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);
        for(lista *p=g[w]; p!=NULL; p=p->next)
         if(!s[p->muchie])
         {
             s[p->muchie]=1;
             stack[++top]=p->nod;
         }
        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;
}