Pagini recente » Cod sursa (job #934041) | Cod sursa (job #849160) | Cod sursa (job #66228) | Cod sursa (job #1487717) | Cod sursa (job #1466508)
#include <fstream>
#include <list>
#include <queue>
#include <stack>
#define nmax 100010
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int N,M,cap[nmax];
bool B[nmax];
list<int> L,G[nmax];
queue<int> Q;
stack<int> S;
void BFS(int v){
Q.push(v); B[v] = 1;
do{
v = Q.front(); Q.pop();
for( list<int>::iterator it = G[v].begin(); it != G[v].end();it++){
if(!B[*it]) Q.push(*it), B[*it] = true;
}
}while(!Q.empty());
}
bool eulerian(){
for(int i = 1;i<=N;i++){
if(cap[i]%2 == 1){
return 0;
}
}
BFS(1);
for(int i = 1;i<=N;i++){
if(!B[i]){
return 0;
}
}
return 1;
}
void sterge(int v,int w){
G[v].pop_front();
for(list<int>::iterator it = G[w].begin();it != G[w].end();it++){
if(*it == v){
G[w].erase(it);
break;
}
}
}
void euler(int v){
int w;
while(!G[v].empty()){
S.push(v);
w = G[v].front();
sterge(v,w);
v = w;
}
}
void rez(){
if(eulerian()){
int v = 1;
do{
euler(v);
v = S.top(); S.pop();
L.push_back(v);
}while(!S.empty());
}else L.push_back(-1);
}
int main(){
fin >> N >> M;
int x,y;
for(int i = 0;i<M;i++){
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
cap[x]++;
cap[y]++;
}
rez();
for(list<int>::iterator it = L.begin(); it != L.end();it++){
fout << *it << ' ';
}
return 0;
}