首页 技术 正文
技术 2022年11月15日
0 收藏 883 点赞 3,694 浏览 1177 个字

题意

三倍经验哇咔咔

#137. 最小瓶颈路 加强版

#6021. 「from CommonAnts」寻找 LCR

#136. 最小瓶颈路

Sol

首先可以证明,两点之间边权最大值最小的路径一定是在最小生成树上

考虑到这题是边权的最大值,直接把重构树建出来

然后查LCA处的权值即可

输入文件过大,需要用RMQ算法求LCA

// luogu-judger-enable-o2
#include<bits/stdc++.h>
const int MAXN = 1e6 + 10;
using namespace std;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, Q, S, tot, dfn[MAXN], rev[MAXN], dep[MAXN], id[MAXN][21], lg2[MAXN], rd[MAXN];
vector<int> v[MAXN];
void dfs(int x, int fa) {
dfn[x] = ++tot; dep[x] = dep[fa] + 1; id[tot][0] = x;
for(int i = 0, to; i < v[x].size(); i++) {
if((to = v[x][i]) == fa) continue;
dfs(to, x);
id[++tot][0] = x;
}
}
void RMQ() {
for(int i = 2; i <= tot; i++) lg2[i] = lg2[i >> 1] + 1;
for(int j = 1; j <= 20; j++) {
for(int i = 1; (i + (1 << j) - 1) <= tot; i++) {
int r = i + (1 << (j - 1));
id[i][j] = dep[id[i][j - 1]] < dep[id[r][j - 1]] ? id[i][j - 1] : id[r][j - 1];
}
}
}
int Query(int l, int r) {
if(l > r) swap(l, r);
int k = lg2[r - l + 1];
return dep[id[l][k]] < dep[id[r - (1 << k) + 1][k]] ? id[l][k] : id[r - (1 << k) + 1][k];
}
int main() {
freopen("a.in", "r", stdin);
N = read(); Q = read(); S = read();
for(int i = 1; i <= N - 1; i++) {
int x = read(), y = read();
v[x].push_back(y); v[y].push_back(x);
}
dfs(S, 0);
RMQ();
while(Q--) {
int x = read(), y = read();
printf("%d\n", Query(dfn[x], dfn[y]));
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,951
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,477
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,290
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,107
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,739
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,773