Cod sursa(job #1427227)

Utilizator ButnaruButnaru George Butnaru Data 1 mai 2015 19:06:17
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <stdio.h>
using namespace std;
struct date {int m; date *next; };
date *t[100001],*a;
int n,m,i,j,rang[100001],x,y;
void euler(int x,int y)
{
    int xx; date *a;
    while (t[x]) {
    xx=t[x]->m;
    t[x]=t[x]->next;
    a=t[xx];
    if (a->m==x) t[xx]=t[xx]->next; else {
    while (a->next->m!=x) a=a->next;
    a->next=a->next->next;
    }
  euler(xx,y+1);
 }
if (y>0) printf("%d ",x);
}
int main(){
freopen("ciclueuler.in","r",stdin);
freopen("ciclueuler.out","w",stdout);
scanf("%d%d",&n,&m);
for (i=1;i<=m;i++){
scanf("%d%d",&x,&y);
rang[x]++; rang[y]++;
a=new date; a->m=x; a->next=t[y]; t[y]=a;
a=new date; a->m=y; a->next=t[x]; t[x]=a;
}
bool ok=true;
for (i=1;i<=n;i++)
    if (rang[i]%2==1) ok=false;
if (ok) euler(1,0); else printf("-1");
return 0;
}