Pagini recente » Istoria paginii runda/oni_11_12_2/clasament | Autentificare | dr_strangelove | Cod sursa (job #2755963) | Cod sursa (job #2794645)
#include <cassert>
#include <cstdio>
#include <unordered_set>
#include <list>
#include <iterator>
#define MAXN 100000
#define MAMM 500000
#define ERASE(de, la) do { \
muchii[de].erase(muchii[de].find(la)); \
muchii[la].erase(muchii[la].find(de)); \
} while (false);
std::unordered_multiset<int> muchii[MAXN];
std::list<int> ret;
inline void
heir (std::list<int>::iterator ins, int vertex) {
//fprintf(stderr, "Starting a cicle from %d\n", vertex + 1);
int i = vertex;
do {
int de = i;
i = *muchii[de].begin();
//fprintf(stderr, "Deleting the edge between %d and %d\n", de + 1, i + 1);
ERASE(de, i);
ret.emplace(ins, i);
//fprintf(stderr, "Adding %d to tour\n", i + 1);
} while (i != vertex);
}
#define BUFLEN 100000
class {
char buf[BUFLEN + 1];
size_t i = BUFLEN;
char
gc () {
if (i == BUFLEN) {
buf[fread_unlocked(buf, 1, BUFLEN, stdin)] = '\n';
i = 0;
}
return buf[i ++];
}
public:
int
ParseInt () {
int ret = 0;
char c;
while ((c = gc()) != ' ' && c != '\n')
ret = (ret << 3) + (ret << 1) + (c & 15);
return ret;
}
} input;
int main () {
int n, m;
int i;
freopen("ciclueuler.in", "r", stdin);
freopen("ciclueuler.out", "w", stdout);
n = input.ParseInt();
m = input.ParseInt();
{
int de, la;
de = input.ParseInt();
la = input.ParseInt();
-- de, -- la;
ret.emplace_front(de);
muchii[de].emplace(la);
muchii[la].emplace(de);
}
for (i = 1; i != m; ++ i) {
int de, la;
de = input.ParseInt();
la = input.ParseInt();
-- de, -- la;
muchii[de].emplace(la);
muchii[la].emplace(de);
}
for (i = 0; i != n; ++ i) {
if (muchii[i].size() % 2) {
puts("-1");
return 0;
}
}
for (auto it = ret.begin(); it != ret.end();) {
if (muchii[*it].size())
heir(std::next(it), *it);
if (muchii[*it].size() == 0)
++ it;
}
ret.pop_back();
if (ret.size() == m) {
for (auto i : ret)
printf("%d ", i + 1);
} else {
puts("-1");
}
return 0;
}