Cod sursa(job #373368)

Utilizator cristikIvan Cristian cristik Data 13 decembrie 2009 17:20:54
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <stdio.h>
#define max 100010
struct lista
{
    int muchie;
    int nod;
    lista *next;
};
lista *g[max];
int n,i,j,stack[max],top,deg[max],m,k,t;
char s[max*5];
void dfs(int nod)
{
    int w;
    stack[++top]=nod;
    s[nod]=1;
    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;
         }

    }
}
void push(int i,int j,int k)
{
    lista *p=new lista;
    p->muchie=k;
    p->nod=i;
    p->next=g[j];
    g[j]=p;
}
int main()
{
    freopen("ciclueuler.in","r",stdin);
    freopen("ciclueuler.out","w",stdout);
    scanf("%d %d\n",&n,&m);
    for(k=1; k<=m; k++)
    {
        scanf("%d %d\n",&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;
}