zoukankan      html  css  js  c++  java
  • 树上差分 模板

    树上差分:

    树的两个性质: 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
    ",y);
        return 0;
    }
  • 相关阅读:
    滚动条去掉
    js 类继承extends
    html标签分类
    Freemarker模板引擎
    关于SpringMVC控制器的一点补充
    Maven的使用
    多层嵌套的json数据
    前后端数据交互之数据接口
    ES6浅谈之Promise
    ES6(阮一峰)学习总结
  • 原文地址:https://www.cnblogs.com/Tyouchie/p/10384251.html
Copyright © 2011-2022 走看看