【图论】树上差分(点差分)

一.题目

输入样例:

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

输出样例:9


二 .分析

我们可以先建一棵树

但我们发现,这样会超时。

所以,我们想到树上差分

三.代码

cpp 复制代码
/*
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
*/

#include<bits/stdc++.h>
#define maxn 500005
using namespace std;
int n,m;
int head[maxn],depth[maxn],p[maxn][25],d[maxn];
struct Edge{
	int u,v,next;
}edge[maxn<<1];
int cnt=0;
void add(int u,int v){
	edge[++cnt]=(Edge){u,v,head[u]}; head[u]=cnt;
}
void dfs(int u,int fa){
	depth[u]=depth[fa]+1;
	p[u][0]=fa;
	for(int i=1;(1<<i)<=depth[u];i++){
		p[u][i]=p[p[u][i-1]][i-1];
	}
	for(int i=head[u];i;i=edge[i].next){
		int v=edge[i].v;
		if(v!=fa){
			dfs(v,u);
		}
	}
}
int lca(int x,int y){
	if(depth[x]<depth[y]) swap(x,y);
	int lg=0;
	while((1<<lg)<=depth[x]) lg++;
	for(int i=lg;i>=0;i--){
		if(depth[x]-(1<<i)>=depth[y]) x=p[x][i];
	}
	if(x==y) return x;
	for(int i=lg;i>=0;i--){
		if(p[x][i]!=p[y][i]){
			x=p[x][i]; y=p[y][i];
		}
	}
	return p[x][0];
}
void dfs2(int u,int fa){
	for(int i=head[u];i;i=edge[i].next){
		int v=edge[i].v;
		if(v!=fa){
			dfs2(v,u);
			d[u]+=d[v];
		}
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n-1;i++){
		int u,v;cin>>u>>v;add(u,v);add(v,u);
	}
	dfs(1,0); //建树 
	while(m--){
		int u,v; cin>>u>>v;
		d[u]++; d[v]++;
		int lc=lca(u,v);
		d[lc]--; d[p[lc][0]]--;
	}
	dfs2(1,0); //sum求原数组 
	int ans=0;
	for(int i=1;i<=n;i++){
		ans=max(ans,d[i]);
	}
	cout<<ans;
	return 0;
}
相关推荐
伊玛目的门徒20 分钟前
试用leetcode之典中典 二数之和问题
java·算法·leetcode
Jerry1 小时前
LeetCode 226. 翻转二叉树
算法
想做小南娘,发现自己是女生喵2 小时前
【无标题】
数据结构·算法
Kx_Triumphs4 小时前
HDU4348 To the moon(主席树区间修改模板)
算法·题解
旖-旎4 小时前
《LeetCode647 回文子串 || LeetCode 5 最长回文子串》
c++·算法·leetcode·动态规划·哈希算法
轻颂呀5 小时前
约瑟夫环问题
算法
科技大视界7 小时前
投资AI项目,传统尽调不够用了——李章虎律师拆解算法、数据、算力三大雷区
人工智能·算法·数据挖掘
郝学胜-神的一滴7 小时前
算法实战:最小k个数——大顶堆的优雅解法
开发语言·数据结构·c++·python·程序人生·算法·排序算法
Irissgwe7 小时前
算法滑动窗口
数据结构·算法