QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#179470#5665. AA Country and King Dreamoonarseny_y#WA 2ms20284kbC++232.4kb2023-09-14 21:36:022023-09-14 21:36:03

Judging History

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

  • [2023-09-14 21:36:03]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:20284kb
  • [2023-09-14 21:36:02]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define int long long

set<int> g[300005];
vector<int> a;

int cur = 0;
int q = -1;


int tin[300005];
int tout[300005];
int bup = 0;

bool check(int v, int u) {
    return tin[v] <= tin[u] && tout[u] <= tout[v];
}

void dfs(int v, int p = -1) {
    g[v].erase(p);
    if (a[cur]) assert(v == a[cur]);
    else a[cur] = v;
    ++cur;
    while (cur < a.size() && a[cur]) {
        if (a[cur] == p) return;
        g[v].erase(a[cur]);
        dfs(a[cur], v);
        a[cur++] = v;
    }
    if (cur < a.size() && !a[cur]) {
        for (auto u: g[v]) {
            if (check(u, a[q])) {
                dfs(u, v);
                g[v].erase(u);
                a[cur++] = v;
            }
        }
    }
    while (!g[v].empty()) {
        assert(a[cur]);
        assert(g[v].count(a[cur]));
        dfs(a[cur], v);
        g[v].erase(a[cur]);
        a[cur++] = v;
    }
}


void cfs(int v, int p = -1) {
    tin[v] = ++bup;
    for (auto u: g[v]) {
        if (u == p) continue;
        cfs(u, v);
    }
    tout[v] = ++bup;
}

int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        cur = 0;
        int n;
        cin >> n;
        a.resize(2 * n - 1);
        for (auto &i: a) cin >> i;
        a.front() = 1, a.back() = 1;
        set<pair<int, int>> x;
        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]) x.insert({min(a[i + 1], a[i]), max(a[i], a[i + 1])});
        for (int i = 0; i < a.size(); ++i) if (a[i]) wh.erase(a[i]);
        for (auto [x, y]: x) g[x].insert(y), g[y].insert(x);
        q = -1;
        for (int i = 0; i < a.size(); ++i) {
            int j = i;
            if (!a[i]) {
                while (!wh.empty()) {
                    g[a[j - 1]].insert(*wh.begin());
                    g[*wh.begin()].insert(a[j - 1]);
                    a[i] = *wh.begin();
                    a[i + 1] = a[j - 1];
                    i += 2;
                    wh.erase(wh.begin());
                }
                q = i;
            }
        }
        if (q == -1) {
            for (auto &i: a) cout << i << ' ';
            cout << '\n';
            continue;
        }
        cfs(1);
        dfs(1);
        for (auto &i: a) cout << i << ' ';
        cout << '\n';
    }
}

详细

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 20284kb

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 4 3 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 

result:

wrong answer 2nd lines differ - expected: '1 2 3 2 4 2 1 5 1', found: '1 2 3 4 3 2 1 5 1 '