想了挺久没有想出来。。 感觉也是个套路, 以后树上两点最长路径要考虑一下把树的直径抠出来,
会发现就可以分类讨论解决问题了。。
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 2e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 998244353; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, m, pa[N], depth[N]; int dia[N], cntdia; int mx[N], mxL[N], mxR[N]; int mxd[N]; int belong[N]; vector<int> G[N]; bool ban[N]; int Log[N]; struct ST { int dp[N][20]; int ty; void build(int n, int b[], int _ty) { ty = _ty; for(int i = -(Log[0]=-1); i < N; i++) Log[i] = Log[i - 1] + ((i & (i - 1)) == 0); for(int i = 1; i <= n; i++) dp[i][0] = ty * b[i]; for(int j = 1; j <= Log[n]; j++) for(int i = 1; i+(1<<j)-1 <= n; i++) dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]); } inline int query(int x, int y) { if(x > y) return 0; int k = Log[y - x + 1]; return ty * max(dp[x][k], dp[y-(1<<k)+1][k]); } } rmqL, rmqR; PII getDia(int u, int fa) { depth[u] = depth[fa] + 1; pa[u] = fa; PII ret = mk(depth[u], u); for(auto &v : G[u]) { if(v == fa) continue; chkmax(ret, getDia(v, u)); } return ret; } void dfs(int u, int fa, int who) { belong[u] = who; depth[u] = depth[fa] + 1; chkmax(mx[who], depth[u]); for(auto &v : G[u]) { if(ban[v] || v == fa) continue; dfs(v, u, who); chkmax(mxd[u], mxd[v] + 1); } } int main() { scanf("%d", &n); for(int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } int A = getDia(1, 0).se; int B = getDia(A, 0).se; while(B) dia[++cntdia] = B, B = pa[B]; for(int i = 1; i <= cntdia; i++) { ban[dia[i]] = true; depth[dia[i]] = 0; belong[dia[i]] = i; } for(int i = 1; i <= cntdia; i++) { for(auto &v : G[dia[i]]) { if(ban[v]) continue; dfs(v, dia[i], i); chkmax(mxd[dia[i]], mxd[v] + 1); } } for(int i = 1; i <= cntdia; i++) { mxL[i] = mx[i] + (i - 1); mxR[i] = mx[i] + (cntdia - i); } rmqL.build(cntdia, mxL, 1); rmqR.build(cntdia, mxR, 1); scanf("%d", &m); while(m--) { int u, v, ans = 0; scanf("%d%d", &u, &v); int bu = belong[u]; int bv = belong[v]; if(bu == bv) { chkmax(ans, min(depth[u], depth[v]) + bu - 1); chkmax(ans, min(depth[u], depth[v]) + (cntdia - bu)); } else { int tmpu = 0, tmpv = 0; if(bu > bv) { swap(bu, bv); swap(u, v); } int du = depth[u]; int dv = depth[v]; int dis = bv - bu; int mdis = (du + dv + dis) / 2; if(mdis < du) { chkmax(tmpv, rmqR.query(1, bv - 1) - (cntdia - bv)); chkmax(tmpv, rmqL.query(bv + 1, cntdia) - (bv - 1)); } else if(du + dis <= mdis) { chkmax(tmpu, rmqR.query(1, bu - 1) - (cntdia - bu)); chkmax(tmpu, rmqL.query(bu + 1, cntdia) - (bu - 1)); } else { int mid = bu + mdis - du; chkmax(tmpu, rmqR.query(1, bu - 1) - (cntdia - bu)); chkmax(tmpu, rmqL.query(bu + 1, mid) - (bu - 1)); chkmax(tmpv, rmqR.query(mid + 1, bv - 1) - (cntdia - bv)); chkmax(tmpv, rmqL.query(bv + 1, cntdia) - (bv - 1)); } chkmax(ans, tmpu + du); chkmax(ans, tmpv + dv); chkmax(ans, mxd[u]); chkmax(ans, mxd[v]); } printf("%d ", ans); } return 0; } /* */