Submission #1363951

#TimeUsernameProblemLanguageResultExecution timeMemory
1363951avighnaBeech Tree (IOI23_beechtree)C++20
0 / 100
899 ms2162688 KiB
#include <bits/stdc++.h>

using namespace std;

vector<int> beechtree(int N, int M, vector<int> P, vector<int> C) {
  vector<vector<int>> adj(N);
  for (int i = 1; i < N; ++i) {
    adj[P[i]].push_back(i);
  }
  vector<int> ans(N);
  // colors, adj sizes
  auto dfs = [&](auto &&self, int u) -> pair<vector<int>, vector<int>> {
    vector<int> cols(M + 1);
    vector<int> adjs = {int(adj[u].size())};
    if (adjs[0] == 0) {
      adjs.pop_back();
    }
    for (auto &i : adj[u]) {
      cols[C[i]]++;
      auto ch = self(self, i);
      for (int j = 1; j <= M; ++j) {
        cols[j] += ch.first[j];
      }
      for (auto &sz : ch.second) {
        adjs.push_back(sz);
      }
    }

    vector<int> oth(N + 1);
    for (int i = 1; i <= M; ++i) {
      oth[0]++, oth[cols[i]]--;
    }
    partial_sum(oth.begin(),oth.end(), oth.begin());
    while (!oth.empty() && oth.back() == 0) {
      oth.pop_back();
    }
    sort(adjs.begin(), adjs.end());
    sort(oth.begin(), oth.end());
    ans[u] = adjs == oth;

    return {cols, adjs};
  };
  dfs(dfs, 0);
  return ans;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...