QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#401458 | #7736. Red Black Tree | phil071128 | TL | 0ms | 6916kb | C++14 | 1.4kb | 2024-04-28 18:53:14 | 2024-04-28 18:53:14 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,multiset<int> >
#define fi first
#define se second
using namespace std;
int read(){
char c=getchar();int h=0,tag=1;
while(!isdigit(c)) tag=(c=='-'?-1:1),c=getchar();
while(isdigit(c)) h=(h<<1)+(h<<3)+(c^48),c=getchar();
return h*tag;
}
void fil(){
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
}
const int N=1e5+505;
vector<int>s[N];
int ans[N],fa[N];
char c[N]; multiset<int>t3;
pii dfs(int x) {
pii tmp;tmp.fi=0,tmp.se.clear();
bool tag=0;
for(int y:s[x]) {
if(y==fa[x]) continue;
pii to=dfs(y); tmp.fi+=to.fi;
if(!tag) {
tmp.se=to.se;tag=1;continue;
}
vector<int>v1,v2;
for(int t:tmp.se) v1.push_back(t);
for(int t:to.se) v2.push_back(t);
t3.clear();
for(int i=0;i<min(v1.size(),v2.size());i++) t3.insert(v1[i]+v2[i]);
tmp.se=t3;
}
int sum=0;
if(c[x]=='0') tmp.se.insert(1); //red
else tmp.se.insert(-1),tmp.fi++;
for(int t:tmp.se) if(t<0) sum+=t;
ans[x]=tmp.fi+sum;
return tmp;
}
void solve() {
int n=read();
for(int i=1;i<=n;i++) fa[i]=0,s[i].clear(),ans[i]=0,c[i]=0;
scanf("%s",c+1);
for(int i=2;i<=n;i++) fa[i]=read(),s[i].push_back(fa[i]),s[fa[i]].push_back(i);
pii x=dfs(1);
for(int i=1;i<=n;i++) {
printf("%d ",ans[i]);
}
printf("\n");
}
int main(){
//fil();
int T=read();
for(int i=1;i<=min(T,6106);i++) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 6916kb
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...