树上差分 模板

树上差分算法解析
本文深入探讨了树上差分算法,通过实例讲解如何在树状结构中更新路径上节点的权重,以解决节点间路径上的流量计算问题。文章详细介绍了算法原理,包括点的差分方法和LCA(最近公共祖先)的应用,并提供了一段C++代码实现。

树上差分:

树的两个性质: 1 任意两个节点之间有且只有一条路径。 2 根节点确定时,一个节点只有一个父亲节点。

1、点的差分;

在一棵n个结点的树中,形容从si走到到ti的要求,求这条路径上的点被经过的次数。显然我们需要用到LCA;

我们需要让cnt[s] + +,让cnt[t] + +,而让他们的cnt[lca] − −, cnt[faher[lca]] − −; 最终统计:cnt[i]+ = ∑ j∈childI cnt[j]。(cnt表示的是当前的点被经过的次数)最后作差的思想可以参考数列差分的思想,或画图可以理解;

给出一道例题:

Farmer John has installed a new system of N−1 pipes to transport milk between the N stalls in his barn (2≤N≤50,000), conveniently numbered 1…N. Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.

FJ is pumping milk between KK pairs of stalls (1≤K≤100,000). For the iith such pair, you are told two stalls sisi and titi, endpoints of a path along which milk is being pumped at a unit rate. FJ is concerned that some stalls might end up overwhelmed with all the milk being pumped through them, since a stall can serve as a waypoint along many of the KK paths along which milk is being pumped. Please help him determine the maximum amount of milk being pumped through any stall. If milk is being pumped along a path from sisi to titi, then it counts as being pumped through the endpoint stalls sisi and titi, as well as through every stall along the path between them.

 

给定一棵有N个点的树,所有节点的权值都为0。

有K次操作,每次指定两个点s,t,将s到t路径上所有点的权值都加一。

请输出K次操作完毕后权值最大的那个点的权值。

也可以参考松鼠的新家例题;

这道题是一个裸的数的差分题目,我们寻找当前点被找到的次数,最后比较取max;

#include<bits/stdc++.h>
using namespace std;
const int N=50005;
char buf[1<<15],*fs,*ft;
inline int read()
{
    int x=0,f=1;  char ch=getchar();
    while(!isdigit(ch))  {if(ch=='-')  f=-1;  ch=getchar();}
    while(isdigit(ch))  {x=(x<<3)+(x<<1)+(ch^48);  ch=getchar();}
    return x*f;
}
struct edge{
    int v,next;
}e[N<<1];
int head[N],k,fa[N][20],dep[N],n,w[N],q;
void add(int u,int v)
{
    e[++k]=(edge){v,head[u]};
    head[u]=k;
}
void dfs(int u,int f,int h)
{
    dep[u]=h;
    fa[u][0]=f;
    int i;
    for(i=head[u];i;i=e[i].next)
    {
        if(e[i].v!=f)
        {
            dfs(e[i].v,u,h+1);
        }
    }
}
void into()
{
    int i,j,u,v;
    n=read();q=read();
    for(i=1;i<n;i++)
    {
        u=read();v=read();
        add(u,v);
        add(v,u);
    }
    dfs(1,0,1);
    for(j=1;(1<<j)<n;j++)
        for(i=1;i<=n;i++)
            fa[i][j]=fa[fa[i][j-1]][j-1];
}
int LCA(int x,int y)
{
    if(dep[x]<dep[y])    swap(x,y);
    int i;
    for(i=19;i>=0;i--)
        if((1<<i)&(dep[x]-dep[y]))
            x=fa[x][i];
    if(x==y)    return x;
    for(i=19;i>=0;i--)
        if(fa[x][i]!=fa[y][i])
            x=fa[x][i],y=fa[y][i];
    return fa[x][0];
}
void work(int u)
{
    int i;
    for(i=head[u];i;i=e[i].next)
    {
        if(e[i].v!=fa[u][0])
        {
            work(e[i].v);
            w[u]+=w[e[i].v];
        }
    }
}
int main()
{
    into();
    int x,y,z;
    while(q--)
    {
        x=read();y=read();
        z=LCA(x,y);
        ++w[x];
        ++w[y];
        --w[z];
        --w[fa[z][0]];
    }
    work(1);
    for(x=1,y=0;x<=n;x++)
    {
        y=max(y,w[x]);
    }
    printf("%d\n",y);
    return 0;
}

 

转载于:https://www.cnblogs.com/Tyouchie/p/10384251.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值