秒切树上查分....(最近一次集训理解的东西)

但是,我敲了半小时才切掉这道题....

我一直迷在了“边差分”和“点差分”的区别上。

所以,先说一下此题,再说一下区别。

首先,想到差分很容易。

然后,按照戴大爷的说法,x++,y++,lca(x,y)-=2;

这是模板,统计的是每条边被经过几次。理解一下,向上前缀和时,lca向上的那条边,会被计算两次,我们既不希望它被记录,也不希望它被记录两次。

所以,要消除前面的影响,就要把它在lca“断掉”,所以只要消除这条边的影响就行了,实现就是-2;

但是,这题,要求的是点的经过数,所以,貌似不太一样了。

继续考虑,怎么消除影响而且不影响lca那个点。

首先,lca要被记录一次,但是边差分时,为了消除影响,我们减了2次。所以,可以想到,-1即可。

但是,上面的点怎么办呢?怎么消除影响呢?

很简单啊,只要把lca的father给删掉,就可以啦。

也就是fa【lca】-1;

于是,只要点差分就可以了。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+;
int n,m;
struct edge
{
int to,next;
}e[maxn];
int head[maxn],cnt;
inline void addedge(int from,int to)
{
e[++cnt].next=head[from];
e[cnt].to=to;
head[from]=cnt;
}
int dep[maxn];
int fa[maxn][];
int son[maxn];
void dfs(int u,int f)
{
dep[u]=dep[f]+;
fa[u][]=f;
son[f]=u;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].to;
if(v==f)
continue;
dfs(v,u);
}
}
int lca(int a,int b)
{
if(dep[a]>dep[b])
swap(a,b);//a<b
for(int i=;i>=;i--)
{
if(dep[b]-(<<i)>=dep[a])
b=fa[b][i];
}
if(a==b)
return a;
for(int i=;i>=;i--)
{
if(fa[a][i]!=fa[b][i])
{
a=fa[a][i];
b=fa[b][i];
}
}
return fa[a][];
}
int dis[maxn],ans;
void dfs2(int u,int f)
{
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].to;
if(v==f)
continue;
dfs2(v,u);
dis[u]+=dis[v];
}
}
int main()
{
//freopen("zdl.in","r",stdin);
//freopen("zdl.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
addedge(x,y);
addedge(y,x);
}
dfs(,);
for(int i=;i<=;i++)
{
for(int j=;j<=n;j++)
{
fa[j][i]=fa[fa[j][i-]][i-];
}
}
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
int t=lca(x,y);
dis[x]++;
dis[y]++;
dis[t]-=;
dis[fa[t][]]-=;
}
dfs2(,);
for(int i=;i<=n;i++)
{
ans=max(dis[i],ans);//printf("%d ",dis[i]);
}
printf("%d",ans);
return ;
}

(完)

P3128 [USACO15DEC]最大流的更多相关文章

  1. 洛谷 P3128 [USACO15DEC]最大流Max Flow-树上差分(点权/点覆盖)(模板题)

    因为徐州现场赛的G是树上差分+组合数学,但是比赛的时候没有写出来(自闭),背锅. 会差分数组但是不会树上差分,然后就学了一下. 看了一些东西之后,对树上差分写一点个人的理解: 首先要知道在树上,两点之 ...

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

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

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

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of  pipes to transport mil ...

  4. 【luogu P3128 [USACO15DEC]最大流Max Flow】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3128 菜 #include <cstdio> #include <cstring> ...

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

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

  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 [树链剖分]

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

  8. luogu 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

    讲解: https://rpdreamer.blog.luogu.org/ci-fen-and-shu-shang-ci-fen #include <bits/stdc++.h> #def ...

  10. P3128 [USACO15DEC]最大流Max Flow

    思路 这个题哪里有那么费脑筋 我们可以树链剖分嘛LCT昨天学的时候睡着了,不是太会 两遍dfs+一个5行的BIT 其实树链剖分学好了对倍增和LCT理解上都有好处 一条路径上的修改 由于一条剖出来的链是 ...

随机推荐

  1. 使用golang插入mysql性能提升经验

    前言 golang可以轻易制造高并发,在某些场景很合适,比如爬虫的时候可以爬的更加高效.但是对应某些场景,如文件读写,数据库访问等IO为瓶颈的场合,就没有什么优势了. 前提基础 1.golang数据库 ...

  2. ES6——箭头函数与普通函数的区别

    ES6标准新增了一种新的函数:Arrow Function(箭头函数). 为什么叫Arrow Function?因为它的定义用的就是一个箭头: 语法: //1.没有形参的时候 let fun = () ...

  3. Spring 注册BeanPostProcessor 源码阅读

    回顾上一篇博客中,在AbstractApplicationContext这个抽象类中,Spring使用invokeBeanFactoryPostProcessors(beanFactory);执行Be ...

  4. Focus on the Good 专注于好的方面

    [1]  Dealing with people is like digging for gold. When you go digging for an ounce of gold, you hav ...

  5. 数据结构中数组反转与STL库Algorithm中的reverse

    数组是个基本的线性数据结构,其实是内存中的一个块,我们可以通过c++的new来分配一个数组 int* a= new int[5]; 然后填数组的每个元素 a[0]=1; a[1]=2; a[2]=6; ...

  6. PHP range

    1.函数的作用:生成范围内的数据 2.函数的参数: @param mixed $start @param mixed $end @param mixed $step 3.例子: <?php $n ...

  7. [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II

    Description Farmer John is continuing to ponder the issue of cows crossing the road through his farm ...

  8. Java基础(十二)lambda表达式

    1.引入lambda表达式的重要性 lambda表达式是一个可传递的代码块,可以在以后执行一次或多次. 在前面的回调部分,有一个例子是,ActionListener类实现了TimePrinter接口并 ...

  9. 简单的Dos Unity操作(debug)

    使用adb命令启动Unity app,eg: adb shell am start -S -a android.intent.action.MAIN -n co.spe3d.sticker/com.u ...

  10. QQ聊天记录分析

    今天我们用R语言来处理一下.我们会用到一下技术:. (1)正则表达式 (2)词频统计 (3)文本可视化 (4)ggplot2绘图 (5)中文分词 一.数据处理 首先我们要讲QQ聊天记录导出成txt文件 ...