【BZOJ3732】【MST】【LCA】Network 题解

来源:互联网 发布:最新淘宝好评返现违规 编辑:程序博客网 时间:2024/05/22 07:55

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

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <set>#include <queue>#include <algorithm>#include <vector>#include <cstdlib>#include <cmath>#include <ctime>#include <stack>#define INF 2147483647#define LL long long#define clr(x) memset(x, 0, sizeof x)#define ms(a, x) memset(x, a, sizeof x)#define digit (ch <  '0' || ch >  '9')#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;template <class T> inline void read(T &x) {    int flag = 1; x = 0;    char ch = getchar();    while(ch <  '0' || ch >  '9') { if(ch == '-')  flag = -1; ch = getchar(); }    while(ch >= '0' && ch <= '9') { x = (x<<1)+(x<<3)+ch-'0'; ch = getchar(); }    x *= flag;}struct edge {    int x,y,f;    bool operator < (const edge &y) const { return f < y.f; }} edges[30300];struct abcd { int to,f,next; } e[30300];int head[15100],tot;int n,m,k;int fa[15100][20],f_max[15100][20],dpt[15100];int belong[15100];inline int find(int x) {    if(!belong[x] || belong[x] == x) return belong[x] = x;    return belong[x] = find(belong[x]);}inline void add(int x, int y, int z) { e[++tot].to = y; e[tot].f = z; e[tot].next = head[x]; head[x] = tot;}void dfs(int x) {    dpt[x] = dpt[fa[x][0]]+1;    for(int i = head[x]; i; i = e[i].next)        if(e[i].to != fa[x][0]) fa[e[i].to][0] = x, f_max[e[i].to][0] = e[i].f, dfs(e[i].to);}int query(int x, int y) {    int re = 0;    if(dpt[x] < dpt[y]) swap(x, y);    for(int j = 14; ~j; j--) if(dpt[fa[x][j]] >= dpt[y])        re = max(re, f_max[x][j]), x = fa[x][j];    if(x == y) return re;    for(int j = 14; ~j; j--) if(fa[x][j] != fa[y][j])        re = max(re, f_max[x][j]), re = max(re, f_max[y][j]), x = fa[x][j], y = fa[y][j];    re = max(re, f_max[x][0]); re = max(re, f_max[y][0]);    return re;}int main() {    read(n); read(m); read(k);    for(int i = 1; i <= m; i++) read(edges[i].x), read(edges[i].y), read(edges[i].f);    sort(edges+1, edges+m+1);    for(int i = 1; i <= m; i++) {        int fx = find(edges[i].x), fy = find(edges[i].y);        if(fx != fy) {            belong[fx] = fy;            add(edges[i].x, edges[i].y, edges[i].f);            add(edges[i].y, edges[i].x, edges[i].f);        }    }    dfs(1);    for(int j = 1; j <= 14; j++)        for(int i = 1; i <= n; i++)            fa[i][j] = fa[fa[i][j-1]][j-1], f_max[i][j] = max(f_max[i][j-1], f_max[fa[i][j-1]][j-1]);    for(int x, y, i = 1; i <= k; i++) read(x), read(y), printf("%d\n",query(x, y));    return 0;}
原创粉丝点击