因为徐州现场赛的G是树上差分+组合数学,但是比赛的时候没有写出来(自闭),背锅。

会差分数组但是不会树上差分,然后就学了一下。

看了一些东西之后,对树上差分写一点个人的理解:

首先要知道在树上,两点之间只有一条路径。树上差分就是在树上用差分数组,因为是在树上的操作,所以要用到lca,因为对于两点a,b,从a到b这一条链就是a-->lca(a,b)-->b,这是一条链。

其次,树上差分的两种操作:一种是对点权的,另一种是对边权的。

对于点权:

在树上将路径的起点a+1和终点b+1,lca(a,b)-1,lca(a,b)的爸爸-1,因为对于点权,从a到b包括lca(a,b),因为lca其实是+2,lca(a,b)多算了一次,所以lca(a,b)-1,但是lca(a,b)往上的爸爸们都没有计算到,所以lca的爸爸-1就可以。总的代码就是

sum[a]++;sum[b]++;sum[lca]--;sum[fa[lca][]]--;

对于边权:

在树上,对于a和b,用深度更深的那个数表示边的编号,对于a和b连接的边,如果a是b的爸爸,那么这条边的编号就是b。

在树上将起点边a+1,终点边b+1,lca(a,b)-2,因为对于边权,从边a到边b,不包括lca(a,b),因为lca(a,b)这一条边并不在从a到b的路径上,但是lca(a,b)其实多算了2次,所以要减去,就是lca(a,b)-2。

代码就是

sum[a]++;sum[b]++;sum[lca]-=;

其他的好像也没什么了,就是dfs的时候别写捞了就可以。

先贴一个关于点权的树上差分。

P3128 [USACO15DEC]最大流Max Flow

题目描述

Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls in his barn (2 \leq N \leq 50,0002≤N≤50,000), conveniently numbered 1 \ldots N1…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 \leq K \leq 100,0001≤K≤100,000). For the iith such pair, you are told two stalls s_isi​ and t_iti​, 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 s_isi​ to t_iti​, then it counts as being pumped through the endpoint stalls s_isi​ and

t_iti​, as well as through every stall along the path between them.

FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N。所有隔间都被管道连通了。

FJ有K(1≤K≤100,000)条运输牛奶的路线,第i条路线从隔间si运输到隔间ti。一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少。

输入输出格式

输入格式:

The first line of the input contains NN and KK.

The next N-1N−1 lines each contain two integers xx and yy (x \ne yx≠y) describing a pipe

between stalls xx and yy.

The next KK lines each contain two integers ss and tt describing the endpoint

stalls of a path through which milk is being pumped.

输出格式:

An integer specifying the maximum amount of milk pumped through any stall in the

barn.

输入输出样例

输入样例#1: 复制

5 10
3 4
1 5
4 2
5 4
5 4
5 4
3 5
4 3
4 3
1 3
3 5
5 4
1 5
3 4
输出样例#1: 复制

9

题目就是关于点权的,树上差分的模板题。直接代码。

代码:

 //洛谷 P3128 [USACO15DEC]最大流Max Flow -树上差分(点的树上差分)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); struct node{
int to,next;
}edge[maxn<<]; int head[maxn<<],sum[maxn],dep[maxn],fa[maxn][],n,m,cnt,ans; int read()//加速挂
{
int res=;
char c=getchar();
while(c<''||c>'') c=getchar();
while(c>=''&&c<='') res=res*+c-'',c=getchar();
return res;
} void add(int x,int y){edge[++cnt].to=y,edge[cnt].next=head[x],head[x]=cnt;}//链式前向星存图 void dfs(int u,int fath)
{
dep[u]=dep[fath]+,fa[u][]=fath;
for (int i=;fa[u][i];++i) fa[u][i+]=fa[fa[u][i]][i];
for (int i=head[u];i;i=edge[i].next){
int v=edge[i].to;
if(v!=fath) dfs(v,u);
}
} int LCA(int u,int v)
{
if(dep[u]>dep[v]) swap(u,v);
for (int i=;i>=;--i) if(dep[u]<=dep[v]-(<<i)) v=fa[v][i];
if(u==v) return u;
for (int i=;i>=;--i) if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];
return fa[u][];
} void Dfs(int u,int fath)//最后的遍历操作
{
for (int i=head[u];i;i=edge[i].next){
int v=edge[i].to;
if(v==fath) continue;
Dfs(v,u);
sum[u]+=sum[v];
}
ans=max(ans,sum[u]);
} int main()
{
n=read(),m=read();
int x,y;
for (int i=;i<n;i++){
x=read(),y=read();
add(x,y);add(y,x);
}
dfs(,);
for (int i=;i<=m;++i){
x=read();y=read();
int lca=LCA(x,y);
++sum[x];++sum[y];--sum[lca];--sum[fa[lca][]];
}
Dfs(,);
printf("%d\n",ans);
return ;
}

溜了,本来昨天就改写的,忘了。

洛谷 P3128 [USACO15DEC]最大流Max Flow-树上差分(点权/点覆盖)(模板题)的更多相关文章

  1. 洛谷P3128 [USACO15DEC]最大流Max Flow(树上差分)

    题意 题目链接 Sol 树上差分模板题 发现自己傻傻的分不清边差分和点差分 边差分就是对边进行操作,我们在\(u, v\)除加上\(val\),同时在\(lca\)处减去\(2 * val\) 点差分 ...

  2. 洛谷 P3128 [ USACO15DEC ] 最大流Max Flow —— 树上差分

    题目:https://www.luogu.org/problemnew/show/P3128 倍增求 lca 也写错了活该第一次惨WA. 代码如下: #include<iostream> ...

  3. 洛谷3128 [USACO15DEC]最大流Max Flow——树上差分

    题目:https://www.luogu.org/problemnew/show/P3128 树上差分.用离线lca,邻接表存好方便. #include<iostream> #includ ...

  4. 洛谷P3128 [USACO15DEC]最大流Max Flow

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...

  5. 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

  6. 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

  7. 洛谷P3128 [USACO15DEC]最大流Max Flow (树上差分)

    ###题目链接### 题目大意: 给你一棵树,k 次操作,每次操作中有 a  b 两点,这两点路上的所有点都被标记一次.问你 k 次操作之后,整棵树上的点中被标记的最大次数是多少. 分析: 1.由于数 ...

  8. P3128 [USACO15DEC]最大流Max Flow (树上差分)

    题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls ...

  9. 洛谷 P3128 [USACO15DEC]最大流Max Flow

    题目描述 \(FJ\)给他的牛棚的\(N(2≤N≤50,000)\)个隔间之间安装了\(N-1\)根管道,隔间编号从\(1\)到\(N\).所有隔间都被管道连通了. \(FJ\)有\(K(1≤K≤10 ...

随机推荐

  1. javascript实现购物车思路

    /* 思路: 第一步:获取所要操作的节点对象 第二步:当页面加载完后,需要计算本地cookie存了多少[个]商品,把个数赋值给count 第三步:为每一个商品对应的添加购物车按钮绑定一个点击事件onc ...

  2. 在浏览器输入网址,Enter之后发生的事情

    简介: 1. 浏览器接收域名 2. 发送域名给DNS,中文名字是域名系统服务器,一般位于ISP(互联网服务提供商,比如我们熟知的联通.移动.电信等) 中.浏览器会首先发给离自己最近的DNS,DNS收到 ...

  3. 网页导出excel文件

    response.setContentType("application/vnd.ms-excel"); response.setHeader("content-disp ...

  4. CCF-20170901

    试题编号:    201709-1 试题名称:    打酱油 时间限制:    1.0s 内存限制:    256.0MB 问题描述 小明带着N元钱去买酱油.酱油10块钱一瓶,商家进行促销,每买3瓶送 ...

  5. Eclipse srever起来时,时间超过45s。

    双击servere的名字,在属性界面上进行修改. 如下图: 修改TimeOut中的值即可.

  6. Linux系统关闭防火墙端口

    1. 打开防火墙端口 # iptables -I INPUT -p tcp --dport -j ACCEPT # iptables -I INPUT -p tcp --dport -j ACCEPT ...

  7. [Luogu 1160] 队列安排

    Luogu 1160 队列安排 链表H2O H2O H2O模板. 太久不写链表,忘干净了,竟调了半个晚上. 保留备用. #include <cstdio> #include <cst ...

  8. py_faster_rcnn识别出来的结果好多红框重叠

    py_faster_rcnn识别出来的结果好多红框重叠, 可以通过调节demo.py中的NMS_THRESH的值进行限制. NMS_THRESH表示非极大值抑制,这个值越小表示要求的红框重叠度越小,0 ...

  9. End to End Sequence Labeling via Bidirectional LSTM-CNNs-CRF论文小结

    本篇论文是卡内基梅隆大学语言技术研究所2016年  arXiv:1603.01354v5 [cs.LG] 29 May 2016 今天先理解一下这个是什么意思:        找到的相关理解:arXi ...

  10. centos7.2进入单用户模式修改密码

    1 - 在启动grub菜单,选择编辑选项启动 2 - 按键盘e键,来进入编辑界面 3 - 找到Linux 16的那一行,将ro改为rw init=/sysroot/bin/sh 4 - 现在按下 Co ...