QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#179710 | #5665. AA Country and King Dreamoon | arseny_y | ML | 0ms | 18372kb | C++23 | 2.6kb | 2023-09-15 02:11:54 | 2023-09-15 02:11:55 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
set<int> g[300005];
vector<int> a;
int l = -1, r = -1, cur = 0;
int tin[300005], tout[300005], x = 0;
bool check(int v, int u) { return tin[v] <= tin[u] && tout[u] <= tout[v]; }
int d[300005];
int p[300005];
void cfs(int v, int p = -1) {
::p[v] = p;
tin[v] = x++;
for (auto u: g[v]) if (u != p) d[u] = d[v] + 1, cfs(u, v);
tout[v] = x++;
}
void fix() { while (cur < a.size() && !a[cur]) ++cur; }
deque<int> add;
deque<int> get_path(int v, int u) {
deque<int> ans;
while (v != u) {
if (check(v, u)) {
for (auto k: g[v]) {
if (check(v, k) && check(k, u)) {
ans.push_back(v = k);
break;
}
}
} else ans.push_back(v = p[v]);
}
return ans;
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) g[i].clear();
a.resize(2 * n - 1);
add.clear();
for (auto &i: a) cin >> i;
a.front() = 1, a.back() = 1;
l = -1, r = -1;
cur = 0;
set<int> wh;
for (int i = 2; i <= n; ++i) wh.insert(i);
for (int i = 0; i + 1 < a.size(); ++i) if (a[i] && a[i + 1]) g[min(a[i], a[i + 1])].insert(max(a[i], a[i + 1]));
for (int i = 1; i < a.size(); ++i) {
if (!a[i]) {
if (l == -1) l = i;
r = i;
} else wh.erase(a[i]);
}
if (l == -1) {
for (auto &i: a) cout << i << ' ';
cout << '\n';
continue;
}
cfs(1);
int nw = a[l - 1];
auto s = get_path(nw, a[r + 1]);
int cur = l;
while (cur <= r || !wh.empty()) {
int next = -1;
if (!s.empty()) next = s.front(), s.pop_front();
if (!wh.empty() && (next == -1 || *wh.begin() < next)) {
int h = *wh.begin();
wh.erase(wh.begin());
a[cur] = h;
++cur;
while (!wh.empty() && *wh.begin() < nw) {
int h1 = *wh.begin();
wh.erase(wh.begin());
a[cur] = h1;
a[cur + 1] = h;
cur += 2;
}
a[cur] = nw;
++cur;
} else {
a[cur] = (nw = next);
++cur;
}
}
for (auto &i: a) cout << i << ' ';
cout << '\n';
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 18372kb
input:
9 5 1 2 3 2 0 2 1 5 1 5 1 2 3 0 0 2 1 5 1 5 1 2 0 0 0 2 1 5 1 5 1 2 0 0 0 0 1 5 1 5 1 0 0 0 0 0 1 5 1 5 1 0 0 0 0 0 0 5 1 5 1 0 0 0 0 0 0 0 1 5 1 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0
output:
1 2 3 2 4 2 1 5 1 1 2 3 2 4 2 1 5 1 1 2 3 2 4 2 1 5 1 1 2 1 3 1 4 1 5 1 1 2 1 3 1 4 1 5 1 1 2 1 3 1 4 1 5 1 1 2 1 3 1 4 1 5 1 1 2 1 3 1 4 1 5 1 1 2 1 3 1 4 1 5 1
result:
ok 9 lines
Test #2:
score: -100
Memory Limit Exceeded
input:
28668 2 0 2 1 2 0 0 1 2 0 0 0 2 1 0 1 2 1 0 0 2 1 2 0 3 0 2 1 3 1 3 0 0 1 3 1 3 0 0 0 3 1 3 0 0 0 0 1 3 0 0 0 0 0 3 1 0 1 3 1 3 1 0 0 3 1 3 1 0 0 0 1 3 1 0 0 0 0 3 1 2 0 3 1 3 1 2 0 0 1 3 1 2 0 0 0 3 1 2 1 0 1 3 1 2 1 0 0 3 1 2 1 3 0 3 0 2 3 2 1 3 0 0 3 2 1 3 0 0 0 2 1 3 1 0 3 2 1 3 1 0 0 2 1 3 1 2 ...