Pagini recente » Cod sursa (job #2193914) | Cod sursa (job #1303347) | Cod sursa (job #2383460) | Cod sursa (job #1854165) | Cod sursa (job #1138277)
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <stack>
#define nmax 100005
#define mmax 500005
using namespace std;
stack <int> st;
vector <int> sol, v[nmax];
int n, m, a, b;
bool has = true;
void euler(int x) {
int w;
st.push(1);
while(!st.empty()) {
int x = st.top();
st.pop();
while(!v[x].empty()) {
w = v[x][0];
v[x][0] = v[x][v[x].size()-1];
v[x].pop_back();
for(int i=0; i<v[w].size(); i++)
if(v[w][i] == x) {
v[w][i] = v[w][v[w].size()-1];
v[w].pop_back();
break;
}
st.push(w);
}
sol.push_back(x);
}
}
int main() {
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
f>>n>>m;
for(int i=1; i<=m; i++) {
f>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
for(int i=1; i<=n; i++) if(v[i].size() % 2 == 1) has = false;
if(has) {
euler(1);
for(int i=0; i<sol.size(); i++) g<<sol[i]<<" ";
g<<"\n";
}
else g<<"-1\n";
return 0;
}