Pagini recente » Istoria paginii utilizator/tudordan | Diferente pentru problema/pocnitoare intre reviziile 38 si 37 | Profil rares404 | Diferente pentru warm-up-2004/solutii intre reviziile 20 si 18 | Cod sursa (job #3150897)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
#define oo 0x3f3f3f3f;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n, m;
vector<pair<int, int>> graph[100001];
vector<int> result;
bool viz[500001];
void read() {
f>>n>>m;
int x, y;
for(int i = 1;i <= m;++i) {
f>>x>>y;
graph[x].push_back({y, i});
graph[y].push_back({x, i});
}
}
void solve() {
for(int i = 1;i <= n;++i) {
if(graph[i].size() & 1) {
g<<-1;
return;
}
}
stack<int> s;
s.push(1);
while(!s.empty()) {
int top = s.top();
if(graph[top].size()) {
auto back = graph[top].back();
if(!viz[back.second]) {
s.push(back.first);
}
viz[back.second] = true;
graph[top].pop_back();
} else {
s.pop();
result.push_back(top);
}
}
for(const auto& node : result) {
g<<node<<" ";
}
}
int main() {
read();
solve();
return 0;
}