Pagini recente » Cod sursa (job #732510) | Clasament emag_2016-incepatori-3 | Cod sursa (job #675681) | Cod sursa (job #2909013) | Cod sursa (job #3245305)
#include <fstream>
#include <vector>
using namespace std;
ifstream cin("ciclueuler.in");
ofstream cout("ciclueuler.out");
struct Info {
int node2;
int id;
Info() = default;
Info(int node2, int id) : node2(node2), id(id) {}
};
int n, m;
vector<vector<Info>> graph;
vector<int> rs;
void read() {
cin >> n >> m;
graph.resize(n + 1);
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(Info(b, i));
graph[b].push_back(Info(a, i));
}
}
void dfs(int node, bool *vis) {
while (!graph[node].empty()) {
if (vis[graph[node].back().id] == true) {
graph[node].pop_back();
continue;
}
vis[graph[node].back().id] = true;
int temp = graph[node].back().node2;
graph[node].pop_back();
dfs(temp, vis);
}
rs.push_back(node);
}
void solve() {
for (int i = 1; i <= n; i++) {
if (graph[i].size() % 2) {
cout << -1;
return;
}
}
bool *vis = new bool[m + 1];
fill(vis, vis + m + 1, false);
dfs(1, vis);
rs.pop_back();
for (auto it : rs) {
cout << it << " ";
}
delete[] vis;
}
int main() {
read();
solve();
return 0;
}