Pagini recente » Cod sursa (job #3130552) | Statistici Plesu Iuliana Oana (oana120) | Cod sursa (job #2823882) | Cod sursa (job #2240590) | Cod sursa (job #2819560)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
const int maxn = 1e5+5, maxm = 5e5+5;
struct xy {
int x, y;
};
int n, m;
vector <xy> v[maxn];
vector <int> ans;
int gr[maxn];
stack <int> st;
bool done[maxm];
void euler() {
int x;
st.push(1);
while(!st.empty()) {
x = st.top();
if(gr[x] != 0) {
if(done[v[x][gr[x]-1].y] == false) {
done[v[x][gr[x]-1].y] = true;
st.push(v[x][gr[x]-1].x);
}
gr[x] --;
} else {
st.pop();
ans.push_back(x);
}
}
for(auto u = ans.begin(); u != ans.end() - 1; u ++) {
g << *u << ' ';
}
g << '\n';
}
int main() {
int i, x, y;
f >> n >> m;
for(i = 1; i <= m; i ++) {
f >> x >> y;
v[x].push_back({y, i});
v[y].push_back({x, i});
gr[x] ++;
gr[y] ++;
}
for(i = 1; i <= n; i ++) {
if(gr[i] % 2 != 0) {
g << "-1\n";
return 0;
}
}
euler();
return 0;
}