QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#179709#5665. AA Country and King Dreamoonarseny_yTL 0ms21468kbC++232.7kb2023-09-15 02:10:452023-09-15 02:10:46

Judging History

你现在查看的是最新测评结果

  • [2023-09-15 02:10:46]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:21468kb
  • [2023-09-15 02:10:45]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define int long long

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: 21468kb

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
Time 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 ...

output:


result: