题意 题目链接 Sol 树上差分模板题 发现自己傻傻的分不清边差分和点差分 边差分就是对边进行操作,我们在\(u, v\)除加上\(val\),同时在\(lca\)处减去\(2 * val\) 点差分是对点操作,我们在\(u, v\)处加上\(val\),在\(lca\)和\(fa[lca]\)处减去\(val\) 就本题而言,属于点差分 #include<bits/stdc++.h> const int MAXN = 1e5 + 10; using namespace std; inline…
题目:https://www.luogu.org/problemnew/show/P3128 倍增求 lca 也写错了活该第一次惨WA. 代码如下: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; ],dep[maxn],s[maxn],mx; bool vis[maxn]; struct N{ int to,nx…
题目:https://www.luogu.org/problemnew/show/P3128 树上差分.用离线lca,邻接表存好方便. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ,M=1e5+; ],nxt[M<<],ans; bool vis[N]; struct Ed{ ,):nxt(n),to(…
因为徐州现场赛的G是树上差分+组合数学,但是比赛的时候没有写出来(自闭),背锅. 会差分数组但是不会树上差分,然后就学了一下. 看了一些东西之后,对树上差分写一点个人的理解: 首先要知道在树上,两点之间只有一条路径.树上差分就是在树上用差分数组,因为是在树上的操作,所以要用到lca,因为对于两点a,b,从a到b这一条链就是a-->lca(a,b)-->b,这是一条链. 其次,树上差分的两种操作:一种是对点权的,另一种是对边权的. 对于点权: 在树上将路径的起点a+1和终点b+1,lca(a,b…
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,…
题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes. FJ is pumping milk…
题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes. FJ is pumping milk…
###题目链接### 题目大意: 给你一棵树,k 次操作,每次操作中有 a  b 两点,这两点路上的所有点都被标记一次.问你 k 次操作之后,整棵树上的点中被标记的最大次数是多少. 分析: 1.由于数据太大,故可以采用树上差分中的点差分来做到 O(1)标记. 2.需要用 tarjan 离线找出两点间的 lca . 3.在树上点差分中,还需要找到 lca 的父亲节点.由于在 tarjan 求 lca 时,并查集中的 pre[lca] 并非一直指向的是 lca 的父亲节点,故需要再开一个 fa[]…
题目描述 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 t…
题目描述 \(FJ\)给他的牛棚的\(N(2≤N≤50,000)\)个隔间之间安装了\(N-1\)根管道,隔间编号从\(1\)到\(N\).所有隔间都被管道连通了. \(FJ\)有\(K(1≤K≤100,000)\)条运输牛奶的路线,第\(i\)条路线从隔间\(s_i\)运输到隔间\(t_i\).一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少. 输入输出格式 输入格式: The first line of the input…
https://www.luogu.org/problem/show?pid=3128 题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-…
裸的树上差分 因为要求点权所以在点上差分即可 #include <cstdio> #include <algorithm> #include <cstring> using namespace std; ; ; ; ,u[MAXN*],v[MAXN*],first[MAXN],next[MAXN*]; int cf[MAXN]; ]; int n,k; void addedge(int ux,int vx){ cnt++; u[cnt]=ux; v[cnt]=vx; n…
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via path…
题意:一棵树,多次给指定链上的节点加1,问最大节点权值 n个点,n-1条边很容易惯性想成一条链,幸好有样例.. 简单的树剖即可!(划去) 正常思路是树上差分,毕竟它就询问一次.. #include<iostream> #include<cstring> #include<cstdio> using namespace std; inline int rd(){ ,f=;char c; :; +c-',c=getchar(); return ret*f; } <&l…
题目链接:https://www.luogu.org/problemnew/show/P3128 菜 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define ll long long #define lson left, mid, rt<<1 #define rson mid+1, right, rt<<1|1 usin…
题目描述 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 t…
讲解: https://rpdreamer.blog.luogu.org/ci-fen-and-shu-shang-ci-fen #include <bits/stdc++.h> #define read read() #define up(i,l,r) for(register int i = (l);i <= (r);i++) #define down(i,l,r) for(register int i = (l);i >= (r);i--) #define traversal…
思路 这个题哪里有那么费脑筋 我们可以树链剖分嘛LCT昨天学的时候睡着了,不是太会 两遍dfs+一个5行的BIT 其实树链剖分学好了对倍增和LCT理解上都有好处 一条路径上的修改 由于一条剖出来的链是连续的,我们要选择数据结构维护 不过这里不用维护太多东西,只是区间+1 我们可以选择常数小,好写的树状数组(从50行的线段树变成5行的 BIT) 而且使得\(O(nlog_{2})\)的算法跑的并不慢 具体就是用差分思想,修改区间[L,R]时 $[1,R] +1 $ \([1,L-1] -1\)达到…
链接一下题目:luoguP3128 [USACO15DEC]最大流Max Flow(树上差分板子题) 如果没有学过树上差分,抠这里(其实很简单的,真的):树上差分总结 学了树上差分,这道题就极其显然了,不就是把每一条运输路线差分进去,那就是板子了啊. 树上差分还是很有用的,比较容易写,这种询问很少的题目去敲那么长(还容易出玄学错误)的树剖很浪费,用树上差分就很快了!(//...微笑...\\) 上一波代码: #include<iostream> #include<cstdlib>…
此类型题目有两种比较常见的做法:树链剖分和树上差分. 本题有多组修改一组询问,因此树上差分会比树链剖分优秀很多. 这里两种方法都进行介绍. 树链剖分和树上差分的本质都是将一颗树转换为一个区间,然后进行操作. 也就是说,先将一颗树变成区间,然后套用线段树/树状数组和差分. 树链剖分的具体流程不多加叙述,可以自己去翻它的模板题. 本题维护区间修改单点查询就OK了,最后输出答案的时候直接遍历所有节点取最大值. 1956ms,9805kb #include<iostream> #include<…
题目描述: Farmer John has installed a new system of N−1N-1N−1 pipes to transport milk between the NNN stalls in his barn (2≤N≤50,0002 \leq N \leq 50,0002≤N≤50,000), conveniently numbered 1-N1 \ldots N1-N. Each pipe connects a pair of stalls, and all stal…
P2936 [USACO09JAN]全流Total Flow 题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that connect the well to the barn. He was surprised to find a wild mess of different…
#\(\mathcal{\color{red}{Description}}\) \(Link\) \(FJ\)给他的牛棚的\(N(2≤N≤50,000)\)个隔间之间安装了\(N-1\)根管道,隔间编号从\(1\)到\(N\).所有隔间都被管道连通了. \(FJ\)有\(K(1≤K≤100,000)\)条运输牛奶的路线,第i条路线从隔间\(s_i\)运输到隔间\(t_i\).一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少.…
树剖LCA+树上差分. 树上差分的基本操作. #include <queue> #include <iostream> #include <cstdio> using namespace std; const int N=100005; int head[N],ecnt,dfn[N],top[N],son[N],fa[N],tim,dep[N],siz[N],rnk[N],nums[N],sum[N]; struct Edge{int to,nxt;}e[N<&l…
题面 显然是树上差分模板题啦,不知道树上差分的童鞋可以去百度一下,很简单. 然后顺带学了一下 tarjan 的 O(N+Q) 离线求LCA的算法 (准确的说难道不应该带个并查集的复杂度吗???) 算法过程具体可以看这里 这里说一下我的理解. 大概可以把所有点分成三类(对于每个dfs的状态):已经遍历到的并且不在dfs栈中的点,已经遍历到并且在dfs栈中的点,未被遍历到的点. 其中,第二种的所有点构成了当前的右链. 然后dfs的过程就是,每个节点的子树遍历完了之后,它自己也就变成了第一种点,我们把…
[Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 353  Solved: 236[Submit][Status][Discuss] Description 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), conveni…
题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes. FJ is pumping milk…
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that connect the well to the barn. He was surprised to find a wild mess of different size pipes connected in an appa…
FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N.所有隔间都被管道连通了. FJ有K(1≤K≤100,000)条运输牛奶的路线,第i条路线从隔间si运输到隔间ti.一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少. 思路: 比较基础的树剖题 对于每条线路 我们维护一个区间最大值的线段树 通过树剖实现每个加1的操作 最后读取总最大值就好 代码: // luogu-judger-enable…
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that connect the well to the barn. He was surprised to find a wild mess of different size pipes connected in an appa…