QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#385426#8134. LCA Countingucup-team3215#WA 2ms3632kbC++201.2kb2024-04-10 19:08:472024-04-10 19:08:47

Judging History

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

  • [2024-04-10 19:08:47]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3632kb
  • [2024-04-10 19:08:47]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

constexpr int N = 2e5;

vector<int> nei[N];

template <typename Q>
auto& getc(Q&& q) {
  struct H: decay_t<Q> { decay_t<Q>::c; };
  return q.*&H::c;
}

priority_queue<int> solve(int v) {
  if (nei[v].empty()) return priority_queue<int>{less<int>{}, {0}};
  if (nei[v].size() == 1) return solve(nei[v][0]);
  vector<priority_queue<int>> ch;
  for (auto u: nei[v]) ch.push_back(solve(u));
  priority_queue<int> ans;
  int mx0 = -1, mx1 = -1;
  for (int i = 0; i < ch.size(); ++i) !~mx0 || ch[mx0].top() < ch[i].top()? mx1 = mx0, mx0 = i: !~mx1 || ch[mx1].top() < ch[i].top()? mx1 = i: i;
  int mrg = ch[mx0].top() + ch[mx1].top() + 1;
  ch[mx0].pop(), ch[mx1].pop();
  mx0 = -1;
  for (int i = 0; i < ch.size(); ++i) !~mx0 || ch[mx0].size() < ch[i].size()? mx0 = i: i;
  ans = move(ch[mx0]);
  ans.push(mrg);
  ch.erase(ch.begin() + mx0);
  for (auto& q: ch) for (auto x: getc(q)) ans.push(x);
  return ans;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n; cin >> n;
  for (int i = 1; i < n; ++i) { int p; cin >> p; nei[--p].push_back(i); }
  int ans = 0;
  for (auto v = getc(solve(0)); auto x: v) {
    cout << ++ans << ' ';
    while (x--) cout << (ans += 2) << ' ';
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3560kb

input:

7
1 1 2 4 2 2

output:

1 3 5 6 

result:

ok 4 number(s): "1 3 5 6"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3596kb

input:

10
1 1 2 2 1 1 1 2 4

output:

1 3 5 6 7 8 9 

result:

ok 7 numbers

Test #3:

score: 0
Accepted
time: 2ms
memory: 3632kb

input:

10
1 2 2 4 5 6 1 2 4

output:

1 3 5 7 8 

result:

ok 5 number(s): "1 3 5 7 8"

Test #4:

score: 0
Accepted
time: 1ms
memory: 3568kb

input:

10
1 2 3 3 3 5 6 4 9

output:

1 3 4 

result:

ok 3 number(s): "1 3 4"

Test #5:

score: -100
Wrong Answer
time: 1ms
memory: 3552kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 4 1 2 2 1 1 1 1 2 1 1 1 1 2 2 1 1 1 1 1 6 3 1 1 1 2 2 1 1 2 1 2 1 3 1 2 1 1 2 1 1 1 1 1 1 1 4 1 5 1 1 1 1 1 2 1 1 2 1 2 1 2 5 3 1 3 1 1 2 1 2 1 1 3 2 1 6 2 1 2 5 1 1 1 3 2 1 2 1 1 1 1 1...

output:

1 3 5 7 8 10 11 13 14 16 17 19 20 22 23 25 26 28 29 31 32 34 35 36 37 38 39 41 42 43 44 46 47 49 50 52 53 55 56 57 58 59 60 61 62 63 64 65 67 68 69 70 71 72 73 74 75 77 78 79 81 82 84 85 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 11...

result:

wrong answer 24th numbers differ - expected: '37', found: '36'