当前位置:网站首页>Max Flow P
Max Flow P
2022-08-09 09:20:00 【Liu Jincheng ljc】
Max Flow P
题目大意
题目大意就是给你一棵树,再给你K次操作将x到yThe values of all points of are added1,Then output the maximum value of all point values.
思路
If this question uses violence, it will definitely be in terms of scopeT(tle)So we have to consider using the idea of difference to do it.
代码
先看代码:
#include<bits/stdc++.h>
#define N 2000005
using namespace std;
int f[N][30],cnt[N],d[N],x,y,n,k,l,ans=-123456789;
vector <int> g[N];
queue <int> q;
void bfs(int t){
// Most recent common ancestor initialization
queue <int> q;
q.push(t);
d[t]=1;
while(q.size()){
// 使用广搜bfs
int x=q.front();
q.pop();
for(int i=0;i<g[x].size();i++){
int y = g[x][i];
if(d[y])continue;
d[y] = d[x]+1;
f[y][0] = x;
for(int j=1;j<=l;j++){
f[y][j] = f[f[y][j-1]][j-1];
}
q.push(y);
}
}
}
int lca(int x,int y){
// 获取x和y的最近公共祖先
if(d[x]>d[y])swap(x,y);
for(int i=l;i>=0;i--) // 让x的层数和yequal number of layers
{
if(d[f[y][i]]>=d[x]){
y = f[y][i];
}
}
if(x==y)return x;
for(int i=l;i>=0;i--){
if(f[x][i]!=f[y][i]){
x = f[x][i],y=f[y][i];
}
}
return f[x][0];// Returns the ancestor number
}
int dfs(int x,int fa){
// 统计最大值
int an=0;
for(int i=0;i<g[x].size();i++){
int y = g[x][i];
if(fa==y)continue;
dfs(y,x);
cnt[x] += cnt[y];
}
ans = max(ans,cnt[x]);
}
signed main(){
// 主函数
ios::sync_with_stdio(false),cin.tie(0),cin.tie(0);
cin >> n >> k;
l = log2(n)+1;
for(int i=1;i<n;i++){
cin >>x>>y;
g[x].push_back(y);
g[y].push_back(x); // 建图
}
bfs(1);
for(int i=1;i<=k;i++){
cin >> x>> y;
int lxy=lca(x,y);
cnt[x]++;cnt[y]++;cnt[lxy]--;cnt[f[lxy][0]]--; // Update the difference of the nodes that need to be updated
}
dfs(1,0);
cout << ans <<endl;
return 0;
}
感谢您的收看
边栏推荐
- lateral view explode的另一种实现方式
- 【Pytorch】安装mish_cuda
- MySQL Leak Detection and Filling (3) Calculated Fields
- TypeScript Brief (1)
- 医院智能3D蓝牙导航导诊系统
- gin清晰简化版curd接口例子
- JS报错-Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on...
- MySQL锁
- [Environmental Construction] tensorrt
- The era of Google Maps is over, how to view high-definition satellite image maps?
猜你喜欢
随机推荐
教你如何免费获取0.1米高精度卫星地图
JVM进程诊断利器——Arthas
TypeScript Brief (1)
js在for循环中按照顺序响应请求
多维度LSTM(长短期记忆)神经网络预测未来存款余额走势
Django实现对数据库数据增删改查(一)
MySQL Checking and Filling Leaks (5) Unfamiliar Knowledge Points
政务中心导航定位系统,让高效率办事成为可能
MySQL创建索引的技巧
全球19级谷歌卫星地图免费查看下载
第1讲 Verilog基础知识
常用SQL server语句
VoLTE基础自学系列 | IMS的业务触发机制
Teach you how to get a 0.1-meter high-precision satellite map for free
这12个GIS软件一个比一个好用
SQL server中的数据类型
进入大厂的面试经验(P7)
学习栈的心得和总结(数组实现)
Onnx - environment build 】 【 tensorrt
gin中简单的curd接口例子