求助,本地AC,提交RE

P3806 【模板】点分治 1

wangjinbo @ 2020-08-29 19:00:52

RT,第一个点本地和在线IDE都能过,交上去RE

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+10;
int n,m;
struct edge{
    int next,to,dis;
}a[maxn<<1];
int head[maxn],cnt;
void add(int x,int y,int z)
{
    a[++cnt].next=head[x];
    a[cnt].to=y;
    a[cnt].dis=z;
    head[x]=cnt;
}
struct point{
    int dis,b;
}p[maxn];
bool cmp(point x,point y){return x.dis<y.dis;}
bool vis[maxn];
int size[maxn];
void dfs_size(int now,int fa)
{
    size[now]=1;
    for(int i=head[now];i;i=a[i].next)
    {
        int u=a[i].to;
        if(u==fa||vis[u])continue;
        dfs_size(u,now);
        size[now]+=size[u];
    }
}
int G;
void dfs_G(int now,int fa,int top)
{
    bool flag=false;
    for(int i=head[now];i;i=a[i].next)
    {
        int u=a[i].to;
        if(u==fa||vis[u])continue;
        if(size[u]>size[top]/2)flag=true;
        dfs_G(u,now,top);
    }   
    if(!flag&&size[now]>size[top]/2)G=now;
}
void dfs_dis(int now,int fa,int top)
{
    for(int i=head[now];i;i=a[i].next)
    {
        int u=a[i].to;
        if(u==fa||vis[u])continue;
        p[u].dis=p[now].dis+a[i].dis;
        p[u].b=top;
        dfs_dis(u,now,top);
    }
}
bool solve(int root,int k){
    memset(size,0,sizeof(size));
    memset(p,0,sizeof(p));
    dfs_size(root,0);
    dfs_G(root,0,root);
    root=G;
    vis[root]=1;
    for(int i=head[root];i;i=a[i].next){
        int u=a[i].to;
        if(vis[u])continue;
        p[u].dis=a[i].dis;
        p[u].b=u;
        dfs_dis(u,root,u);
    }
    sort(p+1,p+1+n,cmp);
    int pos1=1,pos2=n;
    while(p[pos1].dis==0)pos1++;
    for(;pos1<=n;pos1++){
        if(p[pos1].dis==k)return true;
        while(p[pos1].dis+p[pos2].dis>k&&pos2)pos2--;
        while(p[pos1].dis+p[pos2].dis==k&&pos2){
            if(p[pos1].b!=p[pos2].b)return true;
            pos2--;
        }
    }
    bool flag=false;
    for(int i=head[root];i;i=a[i].next)
    {
        int u=a[i].to;
        if(vis[u])continue;
        if(solve(u,k))flag=1;
    }
    return flag;
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<n;i++)
    {
        int x,y,z;
        scanf("%d %d %d",&x,&y,&z);
        add(x,y,z);add(y,x,z);
    }
    for(int i=1;i<=m;i++)
    {
        memset(vis,0,sizeof(vis));
        int k;
        scanf("%d",&k);
        if(solve(1,k))printf("AYE\n");
        else printf("NAY\n");
    }
    return 0;
}

by Gary88 @ 2021-01-18 19:17:15

楼主解决了吗?

我也遇到了这个问题


by Alan_Zhao @ 2021-02-03 14:18:48

@wangjinbo stO


by wangjinbo @ 2021-02-03 14:36:48

@Alan_Zhao 您这是把哪年的帖子翻出来了。。。


by Alan_Zhao @ 2021-02-03 14:37:55

@wangjinbo 20 年的


|