QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#772600#8134. LCA Countingucup-team3646WA 1ms3764kbC++202.7kb2024-11-22 20:38:122024-11-22 20:38:24

Judging History

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

  • [2024-11-22 20:38:24]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3764kb
  • [2024-11-22 20:38:12]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, l, r) for (ll i = l; i < r; i++)

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;

#define all(A) A.begin(), A.end()
#define elif else if
using pii = pair<ll, ll>;

bool chmin(auto &a, const auto &b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto &a, const auto &b) { return a < b ? a = b, 1 : 0; }

struct IOSetup
{
  IOSetup()
  {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} iosetup;

template <class T>
void print(vector<T> a)
{
  for (auto x : a)
    cerr << x << ' ';
  cout << endl;
}

void print(auto x) { cout << x << endl; }

template <class Head, class... Tail>
void print(Head &&head, Tail &&...tail)
{
  cout << head << ' ';
  print(forward<Tail>(tail)...);
}

int main()
{
  int n;
  cin >> n;
  vector<int> par(n, -1);
  vvi child(n);
  rep2(v, 1, n)
  {
    cin >> par[v];
    par[v]--;
    child[par[v]].push_back(v);
  }
  vi S(n), leaf(n, 0), dp(n, 0);
  vector<vector<pair<int, int>>> szs(n);
  auto dfs = [&](auto dfs, int v) -> int
  {
    if (child[v].size() == 0)
    {
      leaf[v] = 1;
      dp[v] = 1;
      return 1;
    }
    for (auto u : child[v])
    {
      szs[v].push_back({dfs(dfs, u), u});
    }
    sort(szs[v].rbegin(), szs[v].rend());
    if (szs.size() == 1)
    {
      dp[v] = szs[v][0].first;
    }
    else
    {
      dp[v] = szs[v][0].first + szs[v][1].first;
    }
    return dp[v];
  };
  dfs(dfs, 0);
  int m = 0;
  rep(i, n) m += leaf[i];
  if (m == 1)
  {
    cout << 1 << endl;
    exit(0);
  }
  priority_queue<pair<int, int>> pq;
  vi ans = {};
  vi todo = {0};
  while (todo.size() > 0 || pq.size() > 0)
  {
    int v;
    if (todo.size() > 0)
    {
      v = todo.back();
      todo.pop_back();
      ans.push_back(1);
    }
    else
    {
      v = pq.top().second;
      pq.pop();
      ans.push_back(0);
    }
    bool flg = true;
    while (true)
    {
      if (leaf[v])
      {
        flg = false;
        break;
      }
      if (child[v].size() == 1)
      {
        v = child[v][0];
      }
      else
      {
        break;
      }
    }
    if (!flg)
      continue;
    rep(i, szs[v].size())
    {
      auto [sz, u] = szs[v][i];
      if (sz == 1)
        continue;
      if (i < 2)
        todo.push_back(u);
      else
        pq.push({sz, u});
    }
  }
  int tmp = 1;
  cout << 1 << " ";
  rep2(i, 1, m)
  {
    if (i - 1 < ans.size())
      tmp += 1 + ans[i - 1];
    else
      tmp++;
    cout << tmp << " ";
  }
  cout << endl;
}

詳細信息

Test #1:

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

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: 0ms
memory: 3764kb

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: 0ms
memory: 3512kb

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: 0ms
memory: 3524kb

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

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 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 10...

result:

wrong answer 6th numbers differ - expected: '10', found: '9'