为什么会T啊

P4114 Qtree1

Katsura_Hinagiku @ 2019-09-18 20:44:18

树剖版子哪里写炸了吗。。。

#include<stdio.h>
#include<string.h>
int head[100005],nxt[200005],weight[200005],pnt[200005],steins[200005],E=0;
int son[100005],sz[100005],fa[100005],dep[100005];
int id[100005],val[100005],top[100005],mp[200005],cnt=0;
int tree[400005];
int n;
char opt[15];
int Max(int a,int b)
{
    if(a<b)return b;
    return a;
}
void add_edge(int a,int b,int c,int d)
{
    pnt[E]=b;
    nxt[E]=head[a];
    weight[E]=c;
    steins[d]=E;
    head[a]=E++;
}
void dfs1(int u,int dpth)
{
    dep[u]=dpth;
    sz[u]=1;
    int maxson=0;
    for(int i=head[u];i!=-1;i=nxt[i])
    {
        int v=pnt[i];
        if(v==fa[u])continue;
        fa[v]=u;
        dfs1(v,dpth+1);
        sz[u]+=sz[v];
        if(sz[v]>maxson)
        {
            maxson=sz[v];
            son[u]=v;
        }
    }
}
void dfs2(int u,int tops)
{
    top[u]=tops;
    id[u]=++cnt;
    for(int i=head[u];i!=-1;i=nxt[i])
    {
        int v=pnt[i];
        if(v==fa[u])
        {
            val[id[u]]=weight[i];
            mp[i]=u;
            break;
        }
    }
    if(!son[u])return;
    dfs2(son[u],son[u]);
    for(int i=head[u];i!=-1;i=nxt[i])
    {
        int v=pnt[i];
        if(v==fa[u]||v==son[u])continue;
        dfs2(v,v);
    }
}
void pushup(int k)
{
    tree[k]=Max(tree[k<<1],tree[k<<1|1]);
}
void build(int l,int r,int k)
{
    if(l==r)
    {
        tree[k]=val[l];
        return;
    }
    int m=(l+r)>>1;
    build(l,m,k<<1);
    build(m+1,r,k<<1|1);
    pushup(k);
}
void modify(int l,int r,int k,int pos,int v)
{
    if(l==r)
    {
        tree[k]=v;
        return;
    }
    int m=(l+r)>>1;
    if(pos<=m)modify(l,m,k<<1,pos,v);
    else modify(m+1,r,k<<1|1,pos,v);
    pushup(k);
}
int query(int L,int R,int l,int r,int k)
{
    if(L<=l&&r<=R)
    {
        return tree[k];
    }
    int m=(l+r)>>1,tmp=0;
    if(L<=m)tmp=Max(tmp,query(L,R,l,m,k<<1));
    if(R>m)tmp=Max(tmp,query(L,R,m+1,r,k<<1|1));
    return tmp;
}
int querychain(int x,int y)
{
    int ans=0;
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]])
        {
            int z=x;
            x=y;
            y=z;
        }
        ans=Max(ans,query(id[top[x]],id[x],1,cnt,1));
        x=fa[top[x]];
    }
    if(x!=y)
    {
        if(dep[x]>dep[y])
        {
            int z=x;
            x=y;
            y=z;
        }
        ans=Max(ans,query(id[x]+1,id[y],1,cnt,1));
    }
    return ans;
}
int main()
{
    memset(head,-1,sizeof(head));
    memset(mp,-1,sizeof(mp));
    scanf("%d",&n);
    for(int i=1;i<n;++i)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        add_edge(u,v,w,i);
        add_edge(v,u,w,i);
    }
    dfs1(1,1);
    dfs2(1,1);
    build(1,cnt,1);
    while(1)
    {
        scanf("%s",opt);
        if(opt[0]=='D')break;
        if(opt[0]=='C')
        {
            int x,y;
            scanf("%d%d",&x,&y);
            modify(1,cnt,1,id[Max(mp[steins[x]],mp[steins[x]^1])],y);
        }
        if(opt[0]=='Q')
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(x==y)
            {
                printf("0\n");
                continue;
            }
            printf("%d\n",querychain(x,y));
        }
    }
    return 0;
}

by Katsura_Hinagiku @ 2019-09-18 20:47:06

只有30pts

吸氧有40pts


by ZhuMingYang @ 2019-09-18 20:49:14

@Katsura_Hinagiku

void dfs2(int u,int tops)
{
    top[u]=tops;
    id[u]=++cnt;
    for(int i=head[u];i!=-1;i=nxt[i])
    {
        int v=pnt[i];
        if(v==fa[u])
        {
            val[id[u]]=weight[i];
            mp[i]=u;
            break;
        }
    }
    if(!son[u])return;
    dfs2(son[u],tops);//这里错了
    for(int i=head[u];i!=-1;i=nxt[i])
    {
        int v=pnt[i];
        if(v==fa[u]||v==son[u])continue;
        dfs2(v,v);
    }
}

by ZhuMingYang @ 2019-09-18 20:50:38

@Katsura_Hinagiku 一般树剖T要不就是dfs1没加上儿子的sz 要不就是你这里错的

不要问我怎么知道的 靠经验


by Katsura_Hinagiku @ 2019-09-18 20:51:09

@ZhuMingYang

看错了

谢谢


|