树上差分——点差分裸题 P3128 [USACO15DEC]最大流Max Flow
讲解:
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_vedge(i) for(register int i = head[u]; i ;i = e[i].nxt)
#define ll long long
using namespace std;
int read
{
int x = , f = ; char ch = getchar();
while(ch < || ch > ) {if(ch == '-')f = -; ch = getchar();}
while(ch >= && ch <=) {x = * x + ch - ;ch = getchar();}
return x * f;
}
//--------------------------------------------------------------------
const int N = ;
int n,k;
struct edge{
int v,nxt;
}e[N<<]; int tot,head[N];
void add(int u,int v) {e[++tot] = (edge){v,head[u]};head[u] = tot;} int dep[N],size[N],fa[N],top[N];
int cnt[N],maxp = ; void dfs(int u)
{
dep[u] = dep[fa[u]]+;
size[u] = ;
top[u] = u;
int hson_id = ,hson_size = ;
traversal_vedge(i)
{
int v = e[i].v;
if(v == fa[u]) continue;
fa[v] = u;
dfs(v);
size[u] += size[v];
if(size[v] > hson_size) hson_id = v,hson_size = size[v];
}
if(hson_id) top[hson_id] = u;
} int find(int u)
{
if(top[u] == u) return u;
top[u] = find(top[u]);
return top[u];
} int lca(int x,int y)
{
if(find(x) != find(y))
{
if(dep[top[x]] > dep[top[y]]) return lca(fa[top[x]],y);
else return lca(x,fa[top[y]]);
}
return dep[x] > dep[y] ? y : x;
} void dfs_tree(int u)
{
traversal_vedge(i)
{
int v = e[i].v;
if(v == fa[u]) continue;
dfs_tree(v);
cnt[u] += cnt[v];
}
maxp = max(maxp,cnt[u]);
} void readdata()
{
n = read; k = read;
up(i,,n-)
{
int u = read, v = read;
add(u,v);
add(v,u);
}
dfs();
up(i,,k)
{
int u = read, v = read;
cnt[u]++; cnt[v]++;
int LCA = lca(u,v);
cnt[LCA]--; cnt[fa[LCA]]--;
}
dfs_tree();
printf("%d\n",maxp);
} int main()
{
freopen("input.txt","r",stdin);
readdata();
return ;
}
树上差分——点差分裸题 P3128 [USACO15DEC]最大流Max Flow的更多相关文章
- P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of pipes to transport mil ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow(树上差分)
题意 题目链接 Sol 树上差分模板题 发现自己傻傻的分不清边差分和点差分 边差分就是对边进行操作,我们在\(u, v\)除加上\(val\),同时在\(lca\)处减去\(2 * val\) 点差分 ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- P3128 [USACO15DEC]最大流Max Flow (树上差分)
题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow (树上差分)
###题目链接### 题目大意: 给你一棵树,k 次操作,每次操作中有 a b 两点,这两点路上的所有点都被标记一次.问你 k 次操作之后,整棵树上的点中被标记的最大次数是多少. 分析: 1.由于数 ...
- luogu P3128 [USACO15DEC]最大流Max Flow (树上差分)
题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls ...
- 洛谷 P3128 [ USACO15DEC ] 最大流Max Flow —— 树上差分
题目:https://www.luogu.org/problemnew/show/P3128 倍增求 lca 也写错了活该第一次惨WA. 代码如下: #include<iostream> ...
- 题解——洛谷P3128 [USACO15DEC]最大流Max Flow
裸的树上差分 因为要求点权所以在点上差分即可 #include <cstdio> #include <algorithm> #include <cstring> u ...
随机推荐
- 搭建zookeeper伪分布式集群
伪分布式集群的意思就是在同一台机子上部署多个zookeeoer,但是他们的端口不一样. 1.安装zookeeper 到/usr/local 2.cd /usr/local/zookeeper 3.cd ...
- java的String的乱码浅析
Java又乱码了,怎么办:乱码了说明编码与解码不一致导致.所以使用统一的编码方式即可. 本文并不是一定能解决乱码,本文主要用来了解jvm默认编码,以及string编码与解码一致性问题. jvm的默认编 ...
- centos7在vmware上安装后不能连接网络的问题。注意虚拟机向导时选择的操作系统...........
在虚拟机上安装时发现网络不可用 在网上查找解决方案一整理天,各种命令代码偿试 无效. 在贴上发现说 Centos7镜像是64位 ,虚拟机操作系统一定要选择centos64位也是必须的. 最后选择重 ...
- 学习笔记:FIS3
http://fis.baidu.com/ FIS3官网 [配环境]: 1.先要安装node.js https://nodejs.org/en/ NODE.js官网(下载这个,下载后运行: http ...
- activemq 的那些事1
#关于事务: activemq 遇到的不能消息确认的问题. Session session = connection.createSession(Boolean.FALSE, Session.AU ...
- leetcode98
class Solution { public: vector<int> V; void postTree(TreeNode* node) { if (node != NULL) { if ...
- leetcode1026
public class Solution { Stack<int> S = new Stack<int>(); ; public void Trace(TreeNode ro ...
- jquery.validate和jquery.form配合实现验证表单后AJAX提交
基础代码其实很简单,之后一点一点扩充.最终代码写在最后. 表单: <form action="@Url.Action("AddColumns","Cont ...
- 爬虫 1 requests 、beautifulsoup
1.requests 1.method 提交方式:post.get.put.delete.options.head.patch 2.url 访问地址 3.params 在url中传递的参数,GET p ...
- 利用DWORD SHOOT实现堆溢出的利用(先知收录)
原文链接:https://xz.aliyun.com/t/4009 1.0 DWORD SHOOT是什么捏? DWORD SHOOT指能够向内存任意位置写入任意数据,1个WORD=4个bytes,即可 ...