Kruskal重构树上\(x\)\(v\)\(lca\)的权值即为它们最长路最小值

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define ll long long
#define u32 unsigned int
#define u64 unsigned long long

#define ON_DEBUGG

#ifdef ON_DEBUGG

#define D_e_Line printf("\n----------\n")
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#include <ctime>
#define TIME() fprintf(stderr, "\ntime: %.3fms\n", clock() * 1000.0 / CLOCKS_PER_SEC)

#else

#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
//char buf[1 << 21], *p1 = buf, *p2 = buf;
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)

#endif

using namespace std;
struct ios{
    template<typename ATP>inline ios& operator >> (ATP &x){
        x = 0; int f = 1; char ch;
        for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
        while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
        x *= f;
        return *this;
    }
}io;

template<typename ATP>inline ATP Max(ATP a, ATP b){
    return a > b ? a : b;
}
template<typename ATP>inline ATP Min(ATP a, ATP b){
    return a < b ? a : b;
}
template<typename ATP>inline ATP Abs(ATP a){
    return a < 0 ? -a : a;
}

const int N = 30007;

struct Edge{
    int nxt, pre;
}e[N << 2];
int head[N], cntEdge;
inline void add(int u, int v){
    e[++cntEdge] = (Edge){ head[u], v}, head[u] = cntEdge;
}
struct node{
    int x, y, w;
    bool operator < (const node &com) const{
        return w < com.w;
    }
}a[N << 1];

int n, m;
int val[N];
namespace KRUS{

    int fa[N];
    inline int Find(int x){
        return x == fa[x] ? x : fa[x] = Find(fa[x]);
    }
    inline void Kruskal(){
        sort(a + 1, a + m + 1);
        int tot = n + 1, lim = n << 1;
        R(i,1,lim) fa[i] = i;//, siz[i] = 1;
        R(i,1,m){
            int p = Find(a[i].x), q = Find(a[i].y);
            if(p != q){
                fa[p] = fa[q] = tot;
                val[tot] = a[i].w;
                add(tot, p), add(tot, q);
//              add(p, tot), add(q, tot);
                if(++tot >= lim) break;
            }
        }
    }

}
namespace TCP{
    int fa[N], top[N], son[N], siz[N], dep[N];
    inline void DFS_First(int u, int father){
        fa[u] = father, siz[u] = 1, dep[u] = dep[father] + 1;
        for(register int i = head[u]; i;i =  e[i].nxt){
            int v = e[i].pre;
            if(v == father) continue;
            DFS_First(v, u);
            siz[u] += siz[v];
            if(siz[v] > siz[son[u]]) son[u] = v;
        }
    }
    inline void DFS_Second(int u, int TP){
        top[u] = TP;
        if(!son[u]) return;
        DFS_Second(son[u], TP);
        for(register int i = head[u]; i; i = e[i].nxt){
            int v = e[i].pre;
            if(v != fa[u] && v !=  son[u])
                DFS_Second(v, v);
        }
    }
    inline int LCA(int x, int y){
        while(top[x] != top[y]){
            if(dep[top[x]] < dep[top[y]]) Swap(x, y);
            x = fa[top[x]];
        }
        return dep[x] < dep[y] ? x : y;
    }
}
int main(){
    freopen("3732Network.in", "r", stdin);
    freopen("3732Network.out", "w", stdout);
    int Q;
    io >> n >> m >> Q;
    R(i,1,m){
        io >> a[i].x >> a[i].y >> a[i].w;
    }
    KRUS::Kruskal();
    int root = (n << 1) - 1; // root is 2 * n - 1
    TCP::DFS_First(root, 0);
    TCP::DFS_Second(root, root);
    while(Q--){
        int u, v;
        io >> u >> v;
        add(u, v);
        add(v, u);
        printf("%d\n", val[TCP::LCA(u, v)]);
    }
    return 0;
}
01-20 01:55