Cod sursa(job #796790)

Utilizator wzrdwzrd tst wzrd Data 12 octombrie 2012 15:45:19
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <iostream>
#include <fstream>
#include <set>
#define DN 100005
using namespace std;

typedef set<int>::iterator is;

int n,m,sol[DN*5],sz,ns=1,st[DN*5],ss;
multiset<int> gr[DN];

void euler() {
    for(st[++ss]=1;ss;) {
        int s=st[ss];
        if(0==gr[s].size()) {
            sol[++sz]=s;
            --ss;
            continue;
        }
        int n=*gr[s].begin();
        gr[s].erase(gr[s].begin());
        gr[n].erase(gr[n].find(s));
        st[++ss]=n;
    }
}

//<parsing>
FILE* fin=fopen("ciclueuler.in","r");
const unsigned maxb=7000000;
char buf[maxb];
unsigned ptr=maxb;

inline unsigned getInt(){
    unsigned nr=0;
	while(buf[ptr]<'0'||'9'<buf[ptr])
		if(++ptr>=maxb)
			fread(buf,maxb,1,fin),ptr=0;
	while('0'<=buf[ptr]&&buf[ptr]<='9'){
		nr=nr*10+buf[ptr]-'0';
		if(++ptr>=maxb)
			fread(buf,maxb,1,fin),ptr=0;
	}
	return nr;
}
//</parsing>

int main()
{
    //ifstream f("ciclueuler.in");
	ofstream g("ciclueuler.out");
	n=getInt(); m=getInt();
	for(int i=0; i<m; ++i) {
	    int a,b;
	    a=getInt(); b=getInt();
	    gr[a].insert(b);
	    gr[b].insert(a);
	    ++st[a]; ++st[b];
	}
	for(int i=1; i<=n; ++i) if(st[i]&1) {
	    g<<"-1";
	    return 0;
	}
	euler();
	for(int i=sz; i>1; --i) g<<sol[i]<<' ';
    return 0;
}