This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |