P3128 [USACO15DEC]Max Flow P ( 树上差分 )

题目链接:点击进入

题目

P3128 [USACO15DEC]Max Flow P ( 树上差分 )
P3128 [USACO15DEC]Max Flow P ( 树上差分 )

思路

学习链接
树上差分应用:
多次对树上的一段路径点权 ( 或者边权 ) 加减 x ,最后询问某个点 ( 或边 ) 权。
前置知识:LCA,差分
点权加:
u , v 路径上所有点权加 x ,pos = lca ( u , v ) ,fa = pre [ pos ] [ 0 ] 为 pos 父亲节点
diff [ u ] += x ,diff [ v ] += x ,diff [ pos ] -= x ,diff [ fa ] -= x ;
边权加:
u , v 路径上所有边权加 x ,pos = lca ( u , v) ,每个点 i 记录的边权为 i 与 i 的父亲节点之间边的权值
diff [ u ] += x ,diff [ v ] += x ,diff [ pos ] -= 2 * x ;

代码

// Problem: P3128 [USACO15DEC]Max Flow P
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3128
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize(3)//O3
//#pragma GCC optimize(2)//O2
#include<iostream>
#include<string>
#include<map>
#include<set>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define base 233 
#define pb push_back
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair<int,int>
#define lowbit(x) x & -x
#define inf 0x3f3f3f3f
//#define int long long
//#define double long double
//#define rep(i,x,y) for(register int i = x; i <= y;++i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai=acos(-1.0);
const int maxn=1e5+10;
const int mod=1e9+7;
const double eps=1e-9;
const int N=5e3+10;
/*--------------------------------------------*/
inline int read()
{
    int k = 0, f = 1 ;
    char c = getchar() ;
    while(!isdigit(c)){if(c == '-') f = -1 ;c = getchar() ;}
    while(isdigit(c)) k = (k << 1) + (k << 3) + c - 48 ,c = getchar() ;
    return k * f ;
}
/*--------------------------------------------*/

int n,m,head[maxn],tot,diff[maxn];
struct node
{
	int to;
	int next;
}edge[maxn];
int deep[maxn],lg[maxn],pre[maxn][30];

void init()
{
	memset(head,-1,sizeof(head));
	memset(deep,0,sizeof(deep));
	memset(lg,0,sizeof(lg));
	memset(pre,0,sizeof(pre));
	memset(diff,0,sizeof(diff));
	tot=0;
}
void add(int u,int v)
{
	edge[tot].to=v;
	edge[tot].next=head[u];
	head[u]=tot++;
}
void dfs1(int pos,int fa)
{
	pre[pos][0]=fa;
	deep[pos]=deep[fa]+1;
	for(int i=1;i<=lg[deep[pos]];i++)
		pre[pos][i]=pre[pre[pos][i-1]][i-1];
	for(int i=head[pos];i!=-1;i=edge[i].next)
		if(edge[i].to!=fa)
			dfs1(edge[i].to,pos);
}
int lca(int x,int y)
{
	if(deep[x]<deep[y]) swap(x,y);
	while(deep[x]>deep[y])
		x=pre[x][lg[deep[x]-deep[y]]-1];
	if(x==y) return x;
	for(int i=lg[deep[x]]-1;i>=0;i--)
		if(pre[x][i]!=pre[y][i])
			x=pre[x][i],y=pre[y][i];
	return pre[x][0];
}
void dfs2(int pos,int fa)
{
	for(int i=head[pos];i!=-1;i=edge[i].next)
	{
		int v=edge[i].to;
		if(v==fa) continue;
		dfs2(v,pos);
		diff[pos]+=diff[v]; 
	}
}

int main() 
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);cout.tie(0);
	init();
	cin>>n>>m;
	for(int i=1;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		add(x,y);
		add(y,x);
	}
	for(int i=1;i<=n;i++)
		lg[i]=lg[i>>1]+1;
	dfs1(1,0);
	while(m--)
	{
		int x,y;
		cin>>x>>y;
		int pos=lca(x,y);
		diff[x]+=1;
		diff[y]+=1;
		diff[pos]+=(-1);
		diff[pre[pos][0]]+=(-1);
	}
	dfs2(1,0);
	int ans=0;
	for(int i=1;i<=n;i++)
		ans=max(ans,diff[i]);
	cout<<ans<<endl;
    return 0;
} 
上一篇:kotlin flow如何结合Android使用场景


下一篇:Spring Cloud Data Flow整合Cloudfoundry UAA服务做权限控制