#include <bits/stdc++.h>
using std::vector;
using std::array;
using std::pair;
using std::tuple;
template <class F> struct RecLambda : private F {
explicit RecLambda(F&& f) : F(std::forward<F>(f)) {}
template <class... Args> decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
std::cin >> N;
array<vector<pair<int, int>>, 2> graph = {};
array<array<int, 2>, 2> count = {};
int src = -1, dst = -1, src_id = -1, dst_id = -1;
for (int i = 0; i < N; ++i) {
int x, a;
std::cin >> x >> a;
if (x == 1) graph[0].emplace_back(a, 1), count[0][1] += 1;
if (x == 2) graph[0].emplace_back(a, 0), count[0][0] += 1;
if (x == 3) graph[1].emplace_back(a, 1), count[1][1] += 1;
if (x == 4) graph[1].emplace_back(a, 0), count[1][0] += 1;
if (x == 5) src = 1, src_id = a;
if (x == 6) src = 0, src_id = a;
if (x == 7) dst = 0, dst_id = a;
if (x == 8) dst = 1, dst_id = a;
}
if ((src == 0) + count[1][0] != (dst == 0) + count[0][1]) {
std::cout << -1 << '\n';
return 0;
}
for (auto& v : graph) {
std::sort(v.begin(), v.end());
}
vector<int> ans;
ans.reserve(N - 2);
RecLambda([&](auto&& dfs, const int u) -> void {
while (!graph[u].empty()) {
const auto [a, v] = graph[u].back();
graph[u].pop_back();
dfs(v);
ans.push_back(a);
}
})(src);
if ((int)ans.size() != N - 2) {
std::cout << -1 << '\n';
return 0;
}
std::reverse(ans.begin(), ans.end());
std::cout << src_id;
for (const int x : ans) {
std::cout << ' ' << x;
}
std::cout << ' ' << dst_id << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
5552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
5416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
2372 KB |
Output is correct |
2 |
Incorrect |
36 ms |
4736 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
2188 KB |
Output is correct |
2 |
Incorrect |
28 ms |
5112 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
5424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
5176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
2316 KB |
Output is correct |
2 |
Incorrect |
32 ms |
5276 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
6376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
5180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
6196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
5192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
5304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |