bzoj 3732: Network

Description

给你N个点的无向图 (1 <= N <= 15,000),记为:1…N。
图中有M条边 (1 <= M <= 30,000) ,第j条边的长度为: d_j ( 1 < = d_j < = 1,000,000,000).

现在有 K个询问 (1 < = K < = 20,000)。
每个询问的格式是:A B,表示询问从A点走到B点的所有路径中,最长的边最小值是多少?

Input

第一行: N, M, K。
第2..M+1行: 三个正整数:X, Y, and D (1 <= X <=N; 1 <= Y <= N). 表示X与Y之间有一条长度为D的边。
第M+2..M+K+1行: 每行两个整数A B,表示询问从A点走到B点的所有路径中,最长的边最小值是多少?

Output

对每个询问,输出最长的边最小值是多少。

Sample Input

6 6 8
1 2 5
2 3 4
3 4 3
1 4 8
2 5 7
4 6 2
1 2
1 3
1 4
2 3
2 4
5 1
6 2
6 1

Sample Output

5
5
5
4
4
7
4
5

HINT

1 <= N <= 15,000
1 <= M <= 30,000
1 <= d_j <= 1,000,000,000
1 <= K <= 15,000

题解

一道kruskal重构树裸题。对刚开始加入的边按照边权从小到大排序,对于不在同一集合的两个点$u$和$v$,建立一个新节点$T$,它们的$fa$数组都指向新节点$T$,点$T$的权值为这条边的权。对于每一个查询,两个点的$LCA$即为答案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<bits/stdc++.h>
#define ps puts("")
#define fi first
#define nd second
#define mset(x) memset((x), 0, sizeof (x))
#define mk make_pair
#define sqr(x) ((x)*(x))
using namespace std;
typedef long long ll;
ll read() {ll x = 0;char f = 1, ch = getchar();while(ch < '0' || ch > '9') {if(ch == '-')f = -1;
ch = getchar();}while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0';ch = getchar();}return x * f;}
void write(ll x) {if(x < 0) x = -x, putchar('-');if(x > 9) write(x / 10);putchar(x % 10 + '0');}
inline void writeln(ll x) {write(x);puts("");}
const int M = 425000, N = 223454;
int n, m, Q, en, head[N], gn;
int fa[N];
struct Edge{
int u, v,nxt;
ll w;
}e[M];
bool cmp(Edge _x, Edge _y) {
return _x.w < _y.w;
}
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void add(int x, int y, ll z) {
e[++en].u = x, e[en].v = y, e[en].w = z, e[en].nxt = head[x], head[x] = en;
e[++en].v = x, e[en].u = y, e[en].w = z, e[en].nxt = head[y], head[y] = en;
}
int f[N][25], d[N];
ll val[N];
int num;
void dfs(int x, int F) {
d[x] = d[F] + 1;
for(int i = head[x]; i;i = e[i].nxt) {
int y = e[i].v;
if(y == F) continue;
f[y][0] = x;
for(int j = 1; j <= 20; ++j)
f[y][j] = f[f[y][j - 1]][j - 1];
dfs(y, x);
}
}
void kruskal() {
num = n;
for(int i = 1; i <= n; ++i)
fa[i] = i;
sort(e + 1, e + 1 + en, cmp);
int tmp = en;
for(int i = 1; i <= tmp; ++i) {
int x = find(e[i].u), y = find(e[i].v);
if(x == y) continue;
val[++num] = e[i].w;
add(num, x, 0);
add(num, y, 0);
fa[x] = fa[y] = fa[num] = num;
}
dfs(num, 0);
}
int ask(int x, int y) {
if(d[x] > d[y]) swap(x, y);
for(int i = 20; i >= 0; --i)
if(d[f[y][i]] >= d[x])
y = f[y][i];
if(x == y) return val[x];
for(int i = 20; i >= 0; --i)
if(f[x][i]!= f[y][i]) {
x = f[x][i];
y = f[y][i];
}
return val[f[x][0]];
}
int main() {
n = read(), m = read(), Q = read();
for(int i = 1; i <= m; ++i) {
int x = read(), y = read();
ll z = read();
e[++en] = (Edge){x, y, 0, z};
}
kruskal();
while(Q--) {
int x = read(), y = read();
writeln(ask(x, y));
}
return 0;
}