Pagini recente » Cod sursa (job #2911428) | Cod sursa (job #1614136) | Cod sursa (job #930194) | Cod sursa (job #2790867) | Cod sursa (job #2859213)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m, x, y, f[500005], t[500005];
vector<int> G[100005], ans, st;
bitset<500005> v; // muchii
int main() {
fin >> n >> m;
for(int i = 1; i <= m; i++) {
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
f[i] = x;
t[i] = y;
}
fin.close();
for(int i = 1; i <= n; i++) {
if(G[i].size() % 2 != 0) { // daca nodul i are grad impar
fout << "-1";
return 0;
}
}
st.push_back(1);
while(!st.empty()) {
int nod = st.back();
if(!G[nod].empty()) {
int e = G[nod].back();
G[nod].pop_back();
if(!v[e]) {
v[e] = true;
int to = ::f[e] ^ ::t[e] ^ nod;
cout << (::t[e]) << "\n";
st.push_back(to);
}
} else {
st.pop_back();
ans.push_back(nod);
}
}
for(unsigned int i = 0; i < ans.size(); i++) {
fout << ans[i] << " ";
}
return 0;
}