萌新刚学点分治,求助

P3806 【模板】点分治 1

Lates @ 2020-03-11 13:47:23

最后一个subtask RE+TLE

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
inline int read(){
    register int x=0,f=0,ch=getchar();
    while('0'>ch||ch>'9')f^=ch=='-',ch=getchar();
    while('0'<=ch&&ch<='9')x=(x<<1)+(x<<3)+(ch^'0'),ch=getchar();
    return f?-x:x;
}
const int MAX=10005,MAXN=10000005;
struct E{
    int e,next,w;
}e[MAX<<1]; 
int cnt=1,head[MAX<<1];
inline void add(int u,int v,int w){
    e[cnt]=(E){v,head[u],w};
    head[u]=cnt++;
}
int siz[MAX],f[MAX],vis[MAX],rt,S;
int dis[MAX],tot,res[MAXN];
inline int _max(int a,int b){
    return a>b?a:b;
}
void Get_root(int x,int fa){
    siz[x]=1;f[x]=0;
    for(register int i=head[x];i;i=e[i].next){
        if(e[i].e!=fa&&!vis[e[i].e]){
            Get_root(e[i].e,x);
            siz[x]+=siz[e[i].e];
            f[x]=_max(f[x],siz[e[i].e]);        
        }
    }
    f[x]=_max(f[x],S-siz[x]);
    if(f[x]<f[rt])rt=x;
}
void Get_dis(int x,int fa,int l){
    dis[++tot]=l;
    for(register int i=head[x];i;i=e[i].next){
        if(e[i].e!=fa&&!vis[e[i].e])Get_dis(e[i].e,x,l+e[i].w);
    }
}
inline void calc(int x,int l,int v){
    tot=0;
    Get_dis(x,0,l);
    for(register int i=1;i<=tot;++i)for(register int j=1;j<=tot;++j)res[dis[i]+dis[j]]+=v;
}
void solve(int x){
    vis[x]=1;
    calc(x,0,1);
    for(register int i=head[x];i;i=e[i].next){
        if(!vis[e[i].e]){
            calc(e[i].e,e[i].w,-1);
            rt=0;S=siz[e[i].e];
            Get_root(e[i].e,0);
            solve(rt);
        }
    }
}
int n,m,s,t,w;
signed main(){
    n=read(),m=read();
    for(register int i=1;i<n;++i){
        s=read(),t=read(),w=read();
        add(s,t,w);add(t,s,w);
    }
    rt=0;f[0]=S=n;
    Get_root(1,0);
    solve(rt);
    for(register int i=1;i<=m;++i){
        w=read();
        printf("%s\n",res[w]?"AYE":"NAY");
    }
    return 0;
}

by Lates @ 2020-03-11 15:00:36

@Alpha @yama是女孩子 谢谢


by Lates @ 2020-03-11 15:53:59

@yama是女孩子 RE好像是calc的两个dis加起来有1e8,爆了我开的res数组


by Early @ 2020-03-11 17:32:33

@chen_03 惊现爆踩学长的学弟!


by Early @ 2020-03-11 17:33:45

貌似楼主也是爆踩学长的学弟

完了,我得逃了


by Lates @ 2020-03-11 22:33:03

@Early Orz学长


by chen_03 @ 2020-03-14 13:28:04

@Early Orz学长


上一页 |