QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#596129 | #7736. Red Black Tree | zhangboju | WA | 521ms | 113448kb | C++14 | 2.0kb | 2024-09-28 15:11:31 | 2024-09-28 15:11:31 |
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];
priority_queue<int> pq[MAXN];
int mdep[MAXN];
void dfs(int u){
if(!T[u].size()){
if(col[u])pq[u].push(1);
else pq[u].push(-1);
return;
}
bool flag=false;
for(auto v: T[u]){
dfs(v);
cnt[u]+=cnt[v];
if(!flag){
swap(pq[u],pq[v]);
mdep[u]=mdep[v];
flag=true;
continue;
}
static int tmp[MAXN];
int len=min(mdep[u],mdep[v]);
mdep[u]=len;
int sum=0;
for(int i=0;i<=len;++i){
if(pq[u].empty()||pq[v].empty())break;
tmp[i]=pq[u].top()+pq[v].top();
pq[u].pop(),pq[v].pop();
}
while(!pq[u].empty())pq[u].pop();
for(int i=0;i<=len;++i){
pq[u].push(tmp[i]);
if(tmp[i]>0)sum+=tmp[i];
tmp[i]=0;
}
if(v==T[u].back())ans[u]=cnt[u]-sum;
}
if(col[u])pq[u].push(1),ans[u]-=(T[u].size()>1);
else pq[u].push(-1);
mdep[u]++;
// priority_queue<int> tmp;
// tmp=pq[u];
// cout<<u<<endl;
// while(!tmp.empty())cout<<tmp.top()<<' ',tmp.pop();
// cout<<endl;
}
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]);
if(i<n)putchar(' ');
}
putchar('\n');
for(int i=1;i<=n;++i){
T[i].clear();
cnt[i]=col[i]=ans[i]=0;
}
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 10572kb
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: 521ms
memory: 113448kb
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:
0 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 -6 0 0 0 0 0 0 0 0 0 -5 0 1 0 0 0 0 2 1 0 0 0 0 0 0 1 0 0 -1 -2 0 0 0 0 0 0 4 3 0 0 2 0 0 0 0 0 0 0 0 0 0 -1 0 -2 0 0 0 0 0 0 0 -2 -3 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 -2 1 0 -9 0 0 0 0 0 0 0 0 0 0 -8 0 0 0 0 0 0 0 0 0...
result:
wrong answer 1st lines differ - expected: '1 1 1 1 0 0 0 0 0 0 0 0', found: '0 1 1 1 0 0 0 0 0 0 0 0'