QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#621467 | #7736. Red Black Tree | SCAU_xyjkanade | RE | 0ms | 0kb | C++14 | 1.3kb | 2024-10-08 14:42:11 | 2024-10-08 14:42:31 |
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n;
int T;
int ans[N];
char s[N];
vector <int> e[N];
vector <int> cf;
pair<int, vector<int> > dfs(int sn)
{
cf.clear();
int v = 0;
for (int i : e[sn])
{
auto [val,depth] = dfs(i);
v += val;
if (cf.empty())
{
cf = depth;
}
else
{
int sz = min(cf.size(), depth.size());
for (int i = 0; i < sz; i++)
{
cf[i] += depth[i];
}
}
}
v += s[sn] - '0';
if (s[sn] == '0')
{
cf.push_back(1);
}
else
{
cf.push_back(-1);
for (int& d : cf) d--;
}
ans[sn] = v + accumulate(begin(cf), end(cf), 0);
return {v, cf};
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> T;
while (T--)
{
cin >> n;
scanf("%s",s + 1);
for (int i = 1; i <= n; i++)
{
e[i].clear();
}
int x;
for (int i = 2; i <= n; i++)
{
cin >> x;
e[x].push_back(i);
}
dfs(1);
for (int i = 1; i <= n; i++)
{
cout << ans[i];
}
cout << endl;
}
return 0;
}
詳細信息
Test #1:
score: 0
Runtime Error
input:
2 9 101011110 1 1 3 3 3 6 2 2 4 1011 1 1 3