QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#595793 | #7736. Red Black Tree | zhangboju | TL | 0ms | 12432kb | C++14 | 2.1kb | 2024-09-28 14:28:26 | 2024-09-28 14:28:29 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int MAXN=100010;
const int INF=1e9;
int n;
char ch[MAXN];
int col[MAXN];
int cnt[MAXN];
int ans[MAXN];
vector<int> T[MAXN];
vector<int> f1[MAXN],f2[MAXN];
int num[MAXN];
int dep[MAXN];
int mdep[MAXN];
void dfs(int u){
if(!T[u].size()){
if(col[u])f1[u].push_back(-1);
else f2[u].push_back(1);
mdep[u]=dep[u];
return;
}
bool flag=false;
int mindep=INF;
for(auto v: T[u]){
dep[v]=dep[u]+1;
dfs(v);
cnt[u]+=cnt[v];
mindep=min(mindep,mdep[v]);
}
static int sta[MAXN];
mdep[u]=mindep;
int len=mindep-dep[u];
for(auto v: T[u]){
int tck=len-1;
for(int i=0;i<f1[v].size();++i){
if(tck<0)break;
sta[i]+=f1[v][i];
tck--;
}
tck-=num[v];
for(int i=0;i<f2[v].size();++i){
if(tck<0)break;
sta[i+f1[v].size()+num[v]]+=f2[v][i];
tck--;
}
}
int i=0;
while(sta[i]<0&&i<len){
f1[u].push_back(sta[i]);
i++;
}
while(sta[i]==0&&i<len){
num[u]++;
i++;
}
if(col[u])f1[u].push_back(-1);
else f2[u].push_back(1);
while(i<len){
f2[u].push_back(sta[i]);
i++;
}
for(int i=0;i<len;++i)sta[i]=0;
int now=cnt[u];
for(int i=0;i<f1[u].size();++i)now+=f1[u][i];
ans[u]=now;
}
int main(){
int testcase;
scanf("%d",&testcase);
while(testcase--){
scanf("%d",&n);
scanf("%s",ch+1);
for(int i=1;i<=n;++i){
col[i]=ch[i]-'0';
cnt[i]=col[i];
}
for(int i=2;i<=n;++i){
int p;
scanf("%d",&p);
T[p].push_back(i);
}
dfs(1);
for(int i=1;i<=n;++i){
printf("%d ",ans[i]);
}
putchar('\n');
for(int i=1;i<=n;++i){
T[i].clear();
cnt[i]=col[i]=ans[i]=num[i]=dep[i]=mdep[i]=0;
f1[i].clear(),f2[i].clear();
}
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 12432kb
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
Time Limit Exceeded
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 1 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 4 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 5 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0...