Pagini recente » Cod sursa (job #1967488) | Borderou de evaluare (job #2008981) | Borderou de evaluare (job #2016416) | Cod sursa (job #2985351) | Cod sursa (job #262773)
Cod sursa(job #262773)
#include<fstream>
#include<stack>
#include<list>
#define MAXN 100009
using namespace std;
list<int> G[MAXN];
typedef list<int>::iterator IT;
int n, m;
int d[MAXN];
stack<int> stiva,rsp;
int main(){
int i, x, y, ok=1;
ifstream f("ciclueuler.in");
f>>n>>m;
for(i=0;i<m;i++){
f>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
d[x]++;d[y]++;
}
f.close();
for(i=1;i<=n;i++)
if(d[i]&1){
ok=0;
break;
}
if(!ok){
ofstream g("ciclueuler.out");
g<<"-1\n";
g.close();
return 0;
}
stiva.push(1);
IT it;
while(!(stiva.empty())){
if(G[stiva.top()].empty()){
rsp.push(stiva.top());
stiva.pop();
}
else {
ok=stiva.top();
stiva.push(*G[ok].begin());
G[ok].pop_front();
x=ok;
ok=stiva.top();
for(it=G[ok].begin();it!=G[ok].end();++it)
if(*it==x){
G[ok].erase(it);
break;
}
}
}
ofstream g("ciclueuler.out");
while(rsp.size()>1){
g<<rsp.top()<<' ';
rsp.pop();
}
g<<'\n';
g.close();
return 0;
}