QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#269163 | #7736. Red Black Tree | ZGS_WZY | TL | 0ms | 9872kb | C++14 | 2.0kb | 2023-11-29 13:31:55 | 2023-11-29 13:31:55 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define rep2(i,j,k) for(int i=j;i>=k;i--)
using namespace std;
template<typename T> void read(T &num){
char c=getchar();T f=1;num=0;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){num=(num<<3)+(num<<1)+(c^48);c=getchar();}
num*=f;
}
template<typename T> void qwq(T x){
if(x>9)qwq(x/10);
putchar(x%10+'0');
}
template<typename T> void write(T x){
if(x<0){x=-x;putchar('-');}
qwq(x);putchar('\n');
}
template<typename T> void chkmin(T &x,T y){x=x<y?x:y;}
char st[100010];
vector<int>v[100010];int L[100010];int minson[100010];
void dfs1(int x){
L[x]=INT_MAX;minson[x]=0;
rep(i,0,(int)v[x].size()-1){
int nop=v[x][i];
dfs1(nop);
if(L[x]>L[nop]+1){
L[x]=L[nop]+1;
minson[x]=nop;
}
}
return;
}
vector<int>f[100010];int ans[100010];
int t1[100010],t2[100010];
void dfs2(int x){
rep(i,0,(int)v[x].size()-1)dfs2(v[x][i]);
if(!minson[x]){
f[x].push_back(1);f[x].push_back(1);
f[x][st[x]-'0']--;
}else{
swap(f[x],f[minson[x]]);
rep(i,0,(int)v[x].size()-1){
int nop=v[x][i];
if(nop==minson[x])continue;
rep(j,0,(int)f[x].size()-1)f[x][j]+=f[nop][j];
}
int len=(int)f[x].size();
rep(i,0,len)t1[i]=t2[i]=INT_MAX;
rep(i,0,len-1){
t1[i+1]=f[x][i]+(st[x]=='0');
t2[i]=f[x][i]+(st[x]=='1');
}
rep(i,0,len-1)f[x][i]=min(t1[i],t2[i]);
f[x].push_back(t1[len]);
}
ans[x]=INT_MAX;
rep(i,0,(int)f[x].size()-1)chkmin(ans[x],f[x][i]);
return;
}
void solve(){
int n;read(n);
rep(i,1,n){v[i].clear();f[i].clear();}
scanf("%s",st+1);
rep(i,2,n){
int x;read(x);
v[x].push_back(i);
}
dfs1(1);
dfs2(1);
rep(i,1,n){
qwq(ans[i]);
putchar((i==n)?'\n':' ');
}
}
int main(){
int T;read(T);
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9872kb
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 0 0 0 0 5 3 ...