QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#309954 | #8134. LCA Counting | ucup-team1469# | WA | 1ms | 3672kb | C++20 | 4.1kb | 2024-01-20 23:06:21 | 2024-01-20 23:06:22 |
Judging History
answer
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tag_and_trait.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// namespace gt = __gnu_pbds;
#define IS_MULTITEST 0
using namespace std;
// #include "angel/math/modint.hpp"
#pragma region Macros
// clang-format off
using ll = long long; using uint = unsigned int; using ull = unsigned long long;
using i32 = int; using i64 = ll; using u32 = uint; using u64 = ull;
using i128 = __int128_t; using u128 = __uint128_t;
using Str = string;
template <class T> using Vec = vector<T>;
template <class T> using RevPriq = priority_queue<T, vector<T>, greater<T>>;
constexpr std::array<std::pair<int, int>, 4> dxy4 = {{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}};
constexpr std::array<std::pair<int, int>, 8> dxy8 = {
{{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}}};
constexpr int inf32 = 1 << 30; constexpr ll inf64 = 1ll << 60;
constexpr char eoln = '\n';
#define L(i, l, r) for (int i = (l); i < (r); ++i)
#define R(i, l, r) for (int i = (r) - 1; i >= (l); --i)
#define ALL(x) (x).begin(), (x).end()
#define mem(a, x) memset((a), (x), sizeof(a))
#define sz(a) (int)((a).size())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
// clang-format on
#pragma endregion
// Coding Space
template <typename G>
struct DoublingLowestCommonAncestor {
const int LOG;
vector<int> dep;
const G &g;
vector<vector<int>> table;
DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) {
table.assign(LOG, vector<int>(g.size(), -1));
}
void dfs(int idx, int par, int d) {
table[0][idx] = par;
dep[idx] = d;
for (auto &to : g[idx]) {
if (to != par) dfs(to, idx, d + 1);
}
}
void build() {
dfs(0, -1, 0);
for (int k = 0; k + 1 < LOG; k++) {
for (int i = 0; i < table[k].size(); i++) {
if (table[k][i] == -1) table[k + 1][i] = -1;
else table[k + 1][i] = table[k][table[k][i]];
}
}
}
int query(int u, int v) {
if (dep[u] > dep[v]) swap(u, v);
for (int i = LOG - 1; i >= 0; i--) {
if (((dep[v] - dep[u]) >> i) & 1) v = table[i][v];
}
if (u == v) return u;
for (int i = LOG - 1; i >= 0; i--) {
if (table[i][u] != table[i][v]) {
u = table[i][u];
v = table[i][v];
}
}
return table[0][u];
}
};
Vec<Vec<int>> Tree;
Vec<int> ord;
void dfs(int v, int &id) {
ord[v] = id++;
for (auto t : Tree[v]) {
dfs(t, id);
}
}
void main_() {
int N;
cin >> N;
Tree.clear();
Tree.resize(N);
Vec<int> P(N);
L(i, 1, N) {
cin >> P[i];
--P[i];
Tree[P[i]].pb(i);
}
DoublingLowestCommonAncestor dlca(Tree);
dlca.build();
int l = 0;
L(i, 0, N) if (Tree[i].empty())++ l;
int m = 0;
set<int> s;
ord.resize(N);
int gomi = 0;
dfs(0, gomi);
Vec<int> leaves;
L(i, 0, N) if (Tree[i].empty()) leaves.pb(i);
sort(ALL(leaves), [&](int a, int b) { return ord[a] < ord[b]; });
int ls = -1;
for (auto t : leaves) {
s.insert(t);
if (ls == -1) {
ls = t;
continue;
} else {
int lca = dlca.query(ls, t);
if (s.find(lca) == s.end()) {
ls = t;
s.insert(lca);
} else {
s.erase(t);
}
}
}
m = (s.size() + 1) / 2;
int ans = -1;
L(i, 0, l) {
if (i < m) ans += 2;
else ans += 1;
cout << ans << (i == l - 1 ? '\n' : ' ');
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
if constexpr (IS_MULTITEST == 0) {
main_();
} else {
// multitest (cf-style)
int T;
cin >> T;
while (T--) {
main_();
cout << flush;
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3648kb
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: 3588kb
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: 3652kb
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: 3584kb
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: 3672kb
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'