当前位置:网站首页>2021icpc Shanghai h.life is a game Kruskal reconstruction tree
2021icpc Shanghai h.life is a game Kruskal reconstruction tree
2022-07-07 23:21:00 【HeartFireY】
H.Life is a Game Kruskal Refactoring tree
Topic analysis
Given a sheet n n n A little bit m m m Undirected graph of strip and edge , as well as q q q A asked . For each inquiry , Given initial point and initial empirical value , Passing through an edge requires the current experience value to be greater than the edge weight , After a point, the point weight is accumulated to the empirical value . Seek the greatest experience you can get .
First, it's easy to prove , For the final answer , The minimum value of the maximum edge weight on all simple paths between two points = The maximum value on the simple path between two points on the minimum spanning tree = K r u s k a Kruska Kruskal Refactoring between two points on the tree L C A LCA LCA A weight
It's so easy to think of building the original drawing K r u s k a l Kruskal Kruskal Refactoring tree , You might as well build a tree for the example first :

Red numbers indicate K r u s k a l Kruskal Kruskal Point weight of reconstruction tree , That is, the minimum value of the maximum edge weight of the original graph . All leaf nodes are nodes of the original drawing , Black numbers represent empirical values , Due to the convenience of later calculation , Sum the empirical values from the leaf node to the root node .
So we have a question , Just from The corresponding leaf node starts to jump up to the ancestor node , Until I can't jump ( Experience value is less than point weight ( Red numbers )), At the same time, we deal with the experience value that can be obtained when reaching each parent node ( Black numbers ), In this way, you only need to jump up all the way to check whether the experience value is greater than K r u s k a l Kruskal Kruskal Just reconstruct the point weight of the tree .
Code
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 2e5 + 10;
int n, m, q;
int pnt[N], val[N];
struct unionfind{
int par[N];
unionfind(int n){
for(int i = 1; i <= n; i++) par[i] = i; }
int find(int x){
return x == par[x] ? x : (par[x] = find(par[x])); }
void merge(int u, int v){
int fau = find(u), fav = find(v);
if(fau != fav) par[fav] = fau;
}
int ismerge(int u, int v){
return find(u) == find(v); }
};
vector<int> g[N];
namespace KR{
struct edge{
int u, v ,w;
const bool operator< (const edge &x) const {
return w < x.w; }
}edges[N];
inline void add_edge(int i, int u, int v, int w){
edges[i] = {
u ,v, w}; }
int kruskal(){
int tot = n, cnt = 0;
unionfind uf(n + 20);
sort(edges + 1, edges + 1 + m);
for(int i = 1; i <= m; i++){
int nu = edges[i].u, nv = edges[i].v, nw = edges[i].w;
int fau = uf.find(nu), fav = uf.find(nv);
if(fau != fav){
val[++tot] = edges[i].w;
uf.par[tot] = uf.par[fau] = uf.par[fav] = tot;
g[fau].emplace_back(tot), g[fav].emplace_back(tot);
g[tot].emplace_back(fau), g[tot].emplace_back(fav);
cnt++;
}
if(cnt == n - 1) break;
}
return tot;
}
}
int fa[N][20];
void dfs0(int u, int ufa){
fa[u][0] = ufa;
for(int i = 1; i < 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
for(auto v : g[u]){
if(v == ufa) continue;
dfs0(v, u);
pnt[u] += pnt[v];
}
}
using KR::add_edge;
using KR::kruskal;
inline void solve(){
cin >> n >> m >> q;
for(int i = 1; i <= n; i++) cin >> pnt[i];
for(int i = 1; i <= m; i++){
int u, v, w; cin >> u >> v >> w;
add_edge(i, u, v ,w);
}
n = kruskal();
dfs0(n, 0);
val[0] = 1e18;
while(q--){
int u, s; cin >> u >> s;
int ans = pnt[u] + s;
while(u != n){
int t = u;
for(int i = 19; i >= 0; i--)
if(val[fa[u][i]] <= ans) u = fa[u][i];
if(t == u) break;
ans = pnt[u] + s;
}
cout << ans << endl;
}
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
solve();
return 0;
}
边栏推荐
- Wechat forum exchange applet system graduation design (2) applet function
- Install Fedora under RedHat
- Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
- Mysql索引优化实战一
- 解决:信息中插入avi格式的视频时,提示“unsupported video format”
- Gee (IV): calculate the correlation between two variables (images) and draw a scatter diagram
- 智慧社區和智慧城市之間有什麼异同
- UE4_UE5蓝图command节点的使用(开启关闭屏幕响应-log-发布全屏显示)
- 648. 单词替换
- Opencv scalar passes in three parameters, which can only be displayed in black, white and gray. Solve the problem
猜你喜欢

LDO稳压芯片-内部框图及选型参数

re1攻防世界逆向

V20变频器手自动切换(就地远程切换)的具体方法示例

Introduction to redis and jedis and redis things

Mysql索引优化实战二

Wechat forum exchange applet system graduation design (2) applet function

Wechat forum exchange applet system graduation design (3) background function

Wechat forum exchange applet system graduation design completion (1) development outline

Wechat forum exchange applet system graduation design completion (7) Interim inspection report

Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle
随机推荐
七月第一周
FPGA基础篇目录
Network security -beef
Freelink open source call center design idea
RE1 attack and defense world reverse
Wechat forum exchange applet system graduation design completion (4) opening report
14、 Two methods of database export and import
Technology at home and abroad people "see" the future of audio and video technology
Network security - install CentOS
【微服务|SCG】gateway整合sentinel
Explain
STL标准模板库(Standard Template Library)一周学习总结
UE4_UE5结合罗技手柄(F710)使用记录
Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle
leetcode-520. 检测大写字母-js
USB(十四)2022-04-12
Two kinds of curves in embedded audio development
ArcGIS: two methods of attribute fusion of the same field of vector elements
Opencv scalar passes in three parameters, which can only be displayed in black, white and gray. Solve the problem
UE4_UE5蓝图command节点的使用(开启关闭屏幕响应-log-发布全屏显示)