QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#783962 | #9806. Growing Tree | Vegetable_Dog | WA | 4ms | 3664kb | C++23 | 2.0kb | 2024-11-26 12:26:27 | 2024-11-26 12:26:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define Rep(i, a, b) for (int i = a; i >= (b); i--)
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second
#define SZ(x) (int)(x.size())
#define lowbit(x) ((x) & -(x))
using db = double;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using i128 = __int128_t;
// Think twice, code once
void solve() {
int h; cin >> h;
int n = (1 << h + 1) - 1;
vi a(n + 1);
rep(i, 2, n + 1) cin >> a[i];
vi val(n + 1);
rep(i, 2, n + 1) val[i] = val[i >> 1] + a[i];
auto lca = [&](int u, int v) {
while (u != v) u >>= 1, v >>= 1;
return u;
};
vi tag(n + 1), vis(n + 1);
vvi son(n + 1);
rep(i, 1 << h, 1 << h + 1) {
rep(j, i + 1, 1 << h + 1) {
int k = lca(i, j);
tag[k] += (val[i] == val[j]);
}
son[i].pb(i);
}
int tot = 0;
Rep(l, h - 1, 0) {
tot++;
rep(i, 1 << l, 1 << l + 1) {
if (tag[i]) {
if (!tot--) { cout << -1 << endl; return; }
for (auto u : son[i << 1]) {
assert(!vis[u]);
vis[u] = 1;
rep(v, 1 << h, 1 << h + 1) if (!vis[v]) {
int w = lca(u, v);
tag[w] -= (val[u] == val[v]);
}
}
son[i << 1].clear();
}
assert(!tag[i]);
for (auto u : son[i << 1]) son[i].pb(u);
for (auto u : son[i << 1 | 1]) son[i].pb(u);
}
}
cout << h - tot << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
cin >> T;
while (T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3608kb
input:
3 2 1 2 4 3 2 1 2 1 2 3 3 2 1 2 1 2 3 3 1 1
output:
1 2 -1
result:
ok 3 number(s): "1 2 -1"
Test #2:
score: -100
Wrong Answer
time: 4ms
memory: 3664kb
input:
94 5 44 65 38 61 64 94 71 53 65 10 24 36 98 74 11 4 5 46 72 34 9 24 37 32 76 29 48 88 17 14 36 4 22 6 71 53 24 61 89 79 39 57 99 61 27 85 99 46 81 75 90 25 16 13 1 87 55 81 56 78 67 2 3 83 3 74 14 45 17 22 41 62 74 25 1 56 22 7 21 73 83 99 3 91 16 53 8 10 49 29 54 81 45 10 12 68 32 9 30 11 99 85 73 ...
output:
3 0 -1 2 0 1 -1 0 0 4 0 0 0 1 2 1 0 2 0 2 0 -1 0 -1 0 0 -1 -1 -1 -1 -1 4 -1 0 4 2 7 -1 -1 -1 1 2 5 0 0 3 -1 1 7 0 -1 2 -1 0 0 0 -1 1 -1 -1 0 0 1 1 -1 0 1 2 0 -1 0 0 1 1 -1 0 -1 0 0 0 -1 3 -1 1 -1 0 0 0 0 1 0 -1 4 3
result:
wrong answer 1st numbers differ - expected: '2', found: '3'