Pagini recente » Cod sursa (job #2510234) | Cod sursa (job #887610) | Cod sursa (job #48407) | Cod sursa (job #193297) | Cod sursa (job #2981227)
#include <bits/stdc++.h>
using namespace std;
bitset <500001> edges;
vector <pair <int, int>> graph[100001];
vector <int> euler;
int grad[100001];
int lastNeighbour[100001];
void dfs (int node) {
for (int i = lastNeighbour[node]; i < graph[node].size(); ++i) {
lastNeighbour[node] += 1;
auto x = graph[node][i];
if (edges[x.second]) {
continue;
}
edges[x.second] = 1;
dfs (x.first);
}
euler.push_back(node);
}
int main() {
ios::sync_with_stdio(false);
ifstream fin ("ciclueuler.in");
ofstream fout ("ciclueuler.out");
fin.tie(nullptr);
fout.tie(nullptr);
int n, m;
fin >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y;
fin >> x >> y;
graph[x].push_back({y, i});
graph[y].push_back({x, i});
grad[x] ^= 1;
grad[y] ^= 1;
}
for (int i =1 ; i <= n; ++i) {
if (grad[i]) {
fout << -1;
return 0;
}
}
dfs (1);
euler.pop_back();
if (euler.size() != m) {
cout << -1;
return 0;
}
for (auto x : euler) {
fout << x << ' ';
}
}