P3313 [SDOI2014]旅行

 

一棵树,其中的点分类,点有权值,在一条链上找到一类点中的最大值或总和;

树链剖分把树变成链;

把每个宗教单开一个线段树,维护区间总和和最大值;

宗教很多,需要动态开点;

 

树链剖分:

void dfs1(int x,int fa)
{
    siz[x]=1;
    father[x]=fa;
    dep[x]=dep[fa]+1;
    for(int p=last[x];p;p=pre[p])
    {
        int v=other[p];
        if(v==fa) continue;
        dfs1(v,x);
        siz[x]+=siz[v];
        if(siz[v]>siz[son[x]]) son[x]=v;
    }
}

void dfs2(int x,int tp)
{
    id[x]=++cnt;
    top[x]=tp;
    if(!son[x]) return ;
    dfs2(son[x],tp);
    for(int p=last[x];p;p=pre[p])
    {
        int v=other[p];
        if(v==father[x]||v==son[x]) continue;
        dfs2(v,v);
    }
}

    dfs1(1,0);
    dfs2(1,1);
View Code

相关文章:

  • 2022-01-12
  • 2021-06-20
  • 2021-10-19
  • 2022-02-10
  • 2021-05-21
  • 2022-12-23
  • 2021-04-23
  • 2021-12-21
猜你喜欢
  • 2022-02-24
  • 2021-09-18
  • 2021-12-12
  • 2021-08-28
  • 2022-12-23
  • 2021-11-22
  • 2021-12-09
相关资源
相似解决方案