QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#644380 | #7736. Red Black Tree | lllei | WA | 409ms | 109208kb | C++20 | 2.0kb | 2024-10-16 13:40:30 | 2024-10-16 13:40:30 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
vector<vector<int>> e(n);
for (int i = 1; i < n; ++i) {
int x;
cin >> x;
--x;
e[x].push_back(i);
}
vector<int> a(n), dep(n), son(n, -1);
auto dfs0 = [&](auto &&self, int u) -> void {
a[u] = (s[u] == '1');
if (int(e[u].size()) == 0) {
dep[u] = 1;
return;
}
for (int v : e[u]) {
self(self, v);
a[u] += a[v];
if (son[u] == -1 || dep[son[u]] > dep[v]) {
son[u] = v;
}
}
dep[u] = dep[son[u]] + 1;
};
dfs0(dfs0, 0);
vector<deque<int>> f(n);
vector<int> ans(n), w(n);
auto dfs1 = [&](auto &&self, int u) -> void {
for (auto v : e[u]) {
self(self, v);
}
if ((int)e[u].size() == 0) {
} else if ((int)e[u].size() == 1) {
f[u].swap(f[son[u]]);
swap(w[u], w[son[u]]);
} else {
// f[u].swap(f[son[u]]);
// swap(w[u], w[v]);
int top = dep[u] - 1;
vector<int> t(top);
for (int v : e[u]) {
auto it = f[v].begin();
for (int i = 0; i < top; ++i, it++) {
t[i] += *it;
}
}
for (auto x : t) {
w[u] += min(x, 0);
f[u].push_back(x);
}
}
if (s[u] == '0') {
f[u].push_back(1);
} else {
f[u].push_front(-1);
w[u]--;
}
ans[u] = a[u] + w[u];
};
dfs1(dfs1, 0);
for (int i = 0; i < n; ++i) {
cout << ans[i] << " \n"[i == n - 1];
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3576kb
input:
2 9 101011110 1 1 3 3 3 6 2 2 4 1011 1 1 3
output:
4 1 2 0 0 0 0 0 0 2 0 0 0
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 409ms
memory: 109208kb
input:
6107 12 000000001000 1 2 3 2 5 4 4 7 3 8 11 19 1100111101111011110 1 2 1 1 4 5 2 4 3 2 2 7 10 2 11 3 15 5 7 0111110 1 1 2 2 1 5 3 000 1 1 7 1000011 1 2 3 3 5 4 7 0001001 1 1 1 3 5 3 8 00111000 1 1 3 2 5 2 7 11 11111110111 1 1 1 4 5 4 5 2 5 1 15 110101101000010 1 2 3 2 1 5 2 5 6 5 8 7 9 14 10 0101000...
output:
1 1 1 1 0 0 0 0 0 0 0 0 6 2 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 2 1 0 0 0 0 0 0 5 0 0 2 1 0 0 0 0 0 0 4 3 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0 0 6 5 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 7 1 0 1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 6 4 ...
result:
wrong answer 3rd lines differ - expected: '1 0 0 0 0 0 0', found: '2 0 0 0 0 0 0'