Pagini recente » Cod sursa (job #987665) | Cod sursa (job #1232215) | Cod sursa (job #862496) | Cod sursa (job #1584238) | Cod sursa (job #2663391)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
struct xy { int x, y; };
const int maxn = 1e5+5, maxm = 5e5+5;
int n, m, x, y, rez;
int fr[maxn], done[maxm];
vector <xy> nod[maxn];
/*void euler(int x) {
if(rez >= m) { return; }
for(auto u : nod[x]) {
if(done[u.y] == 0) {
done[u.y] = 1;
euler(u.x);
}
}
if(rez < m) {
g << x << ' '; rez ++;
}
}*/
/*stack <xy> st;
//stack <int> strez;
vector <int> vecrez;
void euler2(int frst) {
st.push({frst, 0});
int x, y;
while(st.empty() == false) {
x = st.top().x; y = st.top().y;
st.pop();
if(done[y] == true) { continue; }
done[y] = true;
for(auto u : nod[x]) {
if(done[u.y] == false) {
//done[u.y] = true;
st.push({u});
}
}
//strez.push(x);
if(vecrez.size() < m)
{ vecrez.push_back(x); }
}
for(int i = 1; i <= m; i ++) {
// g << strez.top() << ' '; strez.pop();
g << vecrez[i - 1] << ' ';
}
}*/
vector <int> st, stans;
void euler3(int now) {
st.push_back(now);
int x, y;
while(!st.empty()) {
x = st.back();
if(!nod[x].empty()) {
y = nod[x].back().y;
if(done[y] == false) {
done[y] = true;
st.push_back(nod[x].back().x);
}
nod[x].pop_back();
} else {
st.pop_back();
stans.push_back(x);
}
}
for(int i = 1; i <= stans.size() - 1; ++ i) {
g << stans[i - 1] << ' ';
}
}
int main()
{
int i;
f >> n >> m;
for(i = 1; i <= m; i ++) {
f >> x >> y;
nod[x].push_back({y, i});
nod[y].push_back({x, i});
fr[x] ++; fr[y] ++;
}
for(i = 1; i <= n; i ++) {
if(fr[i] % 2 == 1) {
g << -1; return 0;
}
}
euler3(1);
f.close(); g.close();
return 0;
}