Cod sursa(job #373312)

Utilizator cristikIvan Cristian cristik Data 13 decembrie 2009 15:20:42
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <stdio.h>
#define max 100005
struct lista
{
    int nod;
    int muchie;
    lista *next;
};
lista *g[max];
int n,m,i,j,k,deg[max];
char s[max];
void dfs(int x)
{
    for(lista *p=g[x]; p!=NULL; p=p->next)
     if(!s[p->muchie])
     {
          s[p->muchie]=1;
          dfs(p->nod);
     }

    printf("%d ",x);
}
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;
}