求hack
查看原帖
求hack
555065
ChrysanthBlossom楼主2023/7/3 18:19

思路是树上倍增,但是第四个点WA了。

不用指出具体问题,给个hack即可,问题我自己找

#include<bits/stdc++.h>
#define maxn 100005
#define ri register int
#define int long long
using namespace std;
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-48;
        ch=getchar();
    }
    return x*f;
}
inline void write(int n){
    if(n<0){
        putchar('-');
        n=-n;
    }
    if(n>9)
        write(n/10);
    putchar(n%10+'0');
}
int n,f[maxn][30],w[maxn],c[maxn];
int tot,dep[maxn],head[maxn],to[maxn],nxt[maxn];
int s[maxn];
inline void add(int u,int v){
    ++tot;
    to[tot]=v;
    nxt[tot]=head[u];
    head[u]=tot;
}
void dfs(int u){
    dep[u]=dep[f[u][0]]+1;
    for(ri i=1;f[u][i-1];i++)f[u][i]=f[f[u][i-1]][i-1];
    for(ri e=head[u];e;e=nxt[e]){
        int v=to[e];
        dfs(v);
    }
}
int getans(int u,int st,int lmt){
    while(st!=-1&&(!f[u][st]||s[u]-s[f[u][st]]>c[u]))st--;
    if(st==-1){
        if(lmt>=s[u]-s[f[u][0]])return 1;
        return 0;
    }
    return dep[u]-dep[f[u][st]]+getans(f[u][st],st-1,lmt-(s[u]-s[f[u][st]]));
}
signed main(){
    n=read();
    for(ri i=2;i<=n;i++){f[i][0]=read();add(f[i][0],i);}
    for(ri i=1;i<=n;i++){w[i]=read();s[i]=s[f[i][0]]+w[i];}
    for(ri i=1;i<=n;i++)c[i]=read();
    dfs(1);
    //for(ri i=1;i<=n;i++)write(s[i]);
    for(ri i=1;i<=n;i++){write(getans(i,20,c[i]));putchar(' ');}
    return 0;
}
2023/7/3 18:19
加载中...