Cod sursa(job #1364520)

Utilizator pitradaPit-Rada Ionel-Vasile pitrada Data 27 februarie 2015 18:22:26
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.68 kb
#include<stdio.h>
struct muchie
{
    int x,leg;
};
muchie a[1000002];
int n,m,t[100002],stiva[500003],z[500003],vf,x,y,i,k;
int main()
{
    FILE *fin, *fout;
    int rc;
    fin=freopen("ciclueuler.in","r",stdin);
    if(!fin)return -1;
    fout=freopen("ciclueuler.out","w",stdout);
    if(!fout)return -1;
    rc=scanf("%i%i",&n,&m);
    if(rc==EOF)return -2;
    k=0;
    for (i=1;i<=m;i++)
    {
        rc=scanf("%i%i",&x,&y);
        if(rc==EOF)return -2;
        //am simulat crearea listelor de vecini pe vectori, fara sa mai folosesc pointeri...

        k++;
        a[k].x=y;
        a[k].leg=t[x];
        t[x]=k;

        if(y!=x)
        {
            k++;
            a[k].x=x;
            a[k].leg=t[y];
            t[y]=k;
        }

    }
    k=0;
    stiva[1]=1;
    vf=1;
    while (vf>0)
    {
        x=stiva[vf];
        if (t[x]>0)//are vecin; mai exista muchie care porneste din x
        {
            i=t[x];
            y=a[i].x;
            vf++;
            stiva[vf]=y;
            t[x]=a[i].leg;//practic se elimina muchia x y
            if(y!=x)
            {
                i=t[y];
                if (a[i].x==x)t[y]=a[i].leg;
                else {
                        //elimin si muchia y x
                         while (a[a[i].leg].x!=x)i=a[i].leg;
                         a[i].leg=a[a[i].leg].leg;
                     }
            }
        }
        else
        {
            k++;
            z[k]=stiva[vf];
            vf--;
        }
    }
    if (k!=m+1 || z[1]!=z[m+1]) { printf("-1");}
       else for (i=1;i<=m;i++)printf("%i ",z[i]);
    fclose(stdout);
    fclose(stdin);
    return 0;
}