QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#179712#5665. AA Country and King Dreamoonarseny_yML 0ms19636kbC++232.6kb2023-09-15 02:13:362023-09-15 02:13:37

Judging History

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

  • [2023-09-15 02:13:37]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:19636kb
  • [2023-09-15 02:13:36]
  • 提交

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; }


vector<int> get_path(int v, int u) {
    vector<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);
        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;
        std::reverse(s.begin(), s.end());
        while (cur <= r || !wh.empty()) {
            int next = -1;
            if (!s.empty()) next = s.back(), s.pop_back();
            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';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 19636kb

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

output:


result: