Cod sursa(job #764242)

Utilizator SmarandaMaria Pandele Smaranda Data 4 iulie 2012 15:32:49
Problema Ciclu Eulerian Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 3.11 kb
#include <cstdio>
#include <cstring>
#include <list>

using namespace std;

struct nod {
    long x;
    nod *next;
};

nod *a[100001];
nod *first[100001];
long l[100001];
long b[100001];
list <long> q;
bool viz[100001];

void create_nod (long i , long val) {
    nod *p;
    long ok=1;
    if (!a[i]) {  //lista vida
        ok=0;
    }
    p=new nod;
    p->x=val;
    if (ok==1)
        a[i]->next=p;
    p->next=p;
    a[i]=p;
    if (ok==0)
        first[i]=p;
}

long  fleury (long &x) {
    long y,search2,okk=1;
    nod *p,*p1;
    p=first[x];
    p1=first[x];
    if (b[x]) {
        b[x]--;
        search2=x;
    }
    else search2=0;
    do {
        y=p->x;
        if ((viz[y]==1 && search2==0) || (viz[y]==1 && search2==y)) {
            if (a[x]==first[x]) {
                a[x]=first[x]=0;
            }
            else
            if (first[x]!=p)
                p1->next=p->next;
            else
                first[x]=p->next;
            return y;
        }
        p1=p;
        p=p->next;
    } while ((p!=a[x]));
    if (a[x]==first[x]) {
        a[x]=first[x]=0;
        okk=0;
        }
    l[x]--;
    y=p->x;
    p1->next=p1;
    if (okk==1)
    a[x]=p1;
    return y;
}

int main () {
	long n,m,x,y,u=0,i,g,j;
	bool ok=1;
	nod *p,*p1;
	char s[101];

	freopen ("ciclueuler.in","r",stdin);
	freopen ("ciclueuler.out","w",stdout);

	scanf("%ld%ld\n",&n,&m);
	for (i=1;i<=m;i++) {
	//	scanf("%ld%ld",&x,&y);
        x=0;
        y=0;
        gets(s);
        g=strlen(s);
        for (j=0;;j++)
            if (s[j]==' ')
                break;
            else x=x*10+s[j]-'0';
        for (j=j+1;j<g;j++)
            y=y*10+s[j]-'0';
        create_nod (x,y);
        if (x==y)
            b[x]++;
        l[x]++;
        l[y]++;
        create_nod (y,x);
	}
	for (i=1;i<=n && ok;i++)
        if (l[i]%2)
            ok=0;
    if (ok==0)
        printf ("-1\n");
    else {
        q.push_back(1);
        viz[1]=1;
        while (!q.empty()) {
            x=q.front();
            if (!a[x]) {
                if (q.size()!=1)
                    printf("%ld ",x);
                q.pop_front();
            }
            else {
                y=fleury(x);
                q.push_front(y);
                viz[y]=1;
                p=p1=first[y];
                ok=1;
                do {
                    if (p->x==x) {
                        if (a[y]==first[y]) {
                            first[y]=a[y]=0;
                        }
                        else
                        if (p!=first[y])
                            p1->next=p->next;
                        else first[y]=p->next;
                        ok=0;
                }
                    p1=p;
                    p=p->next;
                }while (p!=a[y] && ok);
                //ULTIMUL NOD !!!
                if (ok==1 && a[y]->x==x) {
                    p1->next=p1;
                    a[y]=p1;
                }
                l[y]--;
            }
        }
        printf ("\n");
    }
	return 0;
}