传送门
题意: 跟定一副加权无向图, q次询问, 每次询问两个点之间的路径中最大边的最小是多少.
思路: 很明显, 这个是求最小瓶颈路, 只不过是多次询问, 那么我们可以知道答案一定实在MST上, 所以先求MST, 然后用LCA预处理这颗树, 就可以做到logn的回答了.
复杂度: O(mlogm + nlogn + qlogn)
AC Code
const int maxn = 1e5+5;
// LCA
int up[maxn][23], maxx[maxn][23];
int deep[maxn], dis[maxn];
int cnt, head[maxn];
int n, m, q;
struct node {
int to, next, w;
}e[maxn<<1];
void init() {
Fill(head,-1); Fill(dis,0);
Fill(up,0); Fill(deep,0);
cnt = 0; Fill(maxx, 0);
}
void add(int u, int v, int w) {
e[cnt] = node{v, head[u], w};
head[u] = cnt++;
}
void dfs(int u,int fa,int d)
{
deep[u] = d + 1;
for(int i = 1 ; i < 20 ; i ++) {
up[u][i] = up[up[u][i-1]][i-1];
maxx[u][i] = max(maxx[up[u][i-1]][i-1], maxx[u][i-1]);
}
for(int i = head[u] ; ~i ; i = e[i].next) {
int to = e[i].to;
if(to == fa) continue;
dis[to] = dis[u] + e[i].w;
up[to][0] = u;
maxx[to][0] = e[i].w;
dfs(to, u, d+1);
}
}
int LCA_BZ(int u,int v)
{
int mx = 0;
if(deep[u] < deep[v]) swap(u,v);
int k = deep[u] - deep[v];
for(int i = 0 ; i < 20 ; i ++) {
if((1<if(u != v) {
for(int i = 19 ; i >= 0 ; i --) {
if(up[u][i] != up[v][i]) {
mx = max(mx, max(maxx[v][i], maxx[u][i]));
u = up[u][i];
v = up[v][i];
}
}
mx = max(mx, max(maxx[u][0], maxx[v][0]));
u = up[u][0];
}
return mx;
}
// MST
struct edge {
int u, v, w;
bool operator < (const edge &_ ) const {
return w < _.w;
}
}s[maxn];
int fa[maxn], r[maxn];
bool vis[maxn];
void init2() {
for (int i = 1 ; i <= n ; i ++) {
fa[i] = i;
r[i] = 1;
}
Fill(vis, 0);
}
int Find(int x) {
return fa[x] == x ? x : fa[x] = Find(fa[x]);
}
bool Un(int x, int y) {
int fx = Find(x);
int fy = Find(y);
if (fx == fy) return false;
if (r[fx] > r[fy]) swap(fx, fy);
fa[fx] = fy;
r[fy] += r[fx];
return true;
}
void kru() {
int cnt = 0;
for (int i = 1 ; i <= m ; i ++) {
if (Un(s[i].u, s[i].v)) {
++ cnt;
add(s[i].u, s[i].v, s[i].w);
add(s[i].v, s[i].u, s[i].w);
vis[i] = 1;
}
if (cnt >= n - 1) break;
}
}
void solve()
{
int f = 0;
while(~scanf("%d%d",&n,&m)) {
if (f) printf("\n"); f = 1;
init(); init2();
for (int i = 1 ; i <= m ; i ++) {
scanf("%d%d%d",&s[i].u, &s[i].v, &s[i].w);
}
sort(s+1, s+1+m);
kru(); up[1][0] = 1; //那根节点的父亲设为他自己.
maxx[1][0] = inf; dfs(1,-1,0);
scanf("%d", &q);
while(q--) {
int s, t;
scanf("%d%d", &s, &t);
printf("%d\n", LCA_BZ(s, t));
}
}
}