Pagini recente » Cod sursa (job #1808959) | Cod sursa (job #1454401) | Cod sursa (job #1847753) | Cod sursa (job #951359) | Cod sursa (job #2163893)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define infile "ciclueuler.in"
#define outfile "ciclueuler.out"
using namespace std;
ifstream in(infile);
ofstream out(outfile);
struct fuck{
int nod;
int nrMuchii;
};
int n, m;
int x, y;
int grad[100005];
vector<int> graph[100005];
stack<int> st;
vector<int> ciclu;
void dfsIteratv(int start)
{
for(st.push(start); !st.empty();){
int x = st.top();
if(!graph[x].size()){
st.pop();
ciclu.push_back(x);
continue;
}
int nextOnSt = graph[x][0];
st.push(nextOnSt);
graph[x].erase(graph[x].begin());
if(x != nextOnSt){
if(graph[nextOnSt][0] == x){
graph[nextOnSt].erase(graph[nextOnSt].begin());
}else{
int off = 0;
for(int p=graph[nextOnSt][0]; p!=x; off++){
p = graph[nextOnSt][off];
}
graph[nextOnSt].erase(graph[nextOnSt].begin()+off-1);
}
}
}
}
int main()
{
in >> n >> m;
for(int i=1; i<=m; i++){
in >> x >> y;
graph[x].push_back(y);
if(x!=y){
graph[y].push_back(x);
}
grad[x]++;
grad[y]++;
}
for(int i=1; i<=n; i++){
if(grad[i] & 1){
out << "-1\n";
return 0;
}
}
dfsIteratv(1);
for(int i=0; i<ciclu.size(); i++){
out << ciclu[i] << ' ';
}
return 0;
}