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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| #include <bits/stdc++.h>
#define x first #define y second #define y1 Y1 #define y2 Y2 #define mp make_pair #define pb push_back
using namespace std;
typedef long long LL; typedef pair<int, int> pii;
template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; } inline int read () { int sum = 0, fl = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1; for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0'; return sum * fl; }
const int Maxn = 1000000 + 100;
int N, M, e, Begin[Maxn], To[Maxn << 1], Next[Maxn << 1], W[Maxn << 1]; int dep[Maxn], Dep[Maxn], anc[22][Maxn], Dis[Maxn]; pii Q[Maxn];
inline void add_edge (int x, int y, int z) { To[++e] = y; Next[e] = Begin[x]; Begin[x] = e; W[e] = z; }
inline void dfs_pre (int x) { dep[x] = dep[anc[0][x]] + 1; for (int i = 1; i <= 20; ++i) anc[i][x] = anc[i - 1][anc[i - 1][x]]; for (int i = Begin[x]; i; i = Next[i]) { int y = To[i]; if (y == anc[0][x]) continue; anc[0][y] = x; Dep[y] = Dep[x] + W[i]; dfs_pre(y); } }
inline int get_lca (int x, int y) { if (dep[x] < dep[y]) swap(x, y); for (int i = 20; i >= 0; --i) if (dep[anc[i][x]] >= dep[y]) x = anc[i][x]; if (x == y) return x; for (int i = 20; i >= 0; --i) if (anc[i][x] != anc[i][y]) x = anc[i][x], y = anc[i][y]; return anc[0][x]; }
inline int get_dis (int x, int y) { return Dep[x] + Dep[y] - 2 * Dep[get_lca(x, y)]; }
int delta[Maxn], now_sum, flag, cnt;
inline void dfs (int x, int f) { if (flag) return ; for (int i = Begin[x]; i; i = Next[i]) { int y = To[i]; if (y == f) continue; dfs(y, x); if (delta[y] == cnt && W[i] >= now_sum) { flag = 1; return ; } delta[x] += delta[y]; } }
inline void Mark (int x, int y) { ++delta[x]; ++delta[y]; delta[get_lca(x, y)] -= 2; }
inline int Check (int now_ans) { flag = 0; memset(delta, 0, sizeof delta); int Max = 0; cnt = 0; for (int i = 1; i <= M; ++i) if (Dis[i] > now_ans) ++cnt,Mark(Q[i].x, Q[i].y), Chkmax(Max, Dis[i]); now_sum = Max - now_ans; dfs(1, 0); return flag; }
inline void Solve () { dfs_pre(1); for (int i = 1; i <= M; ++i) Q[i].x = read(), Q[i].y = read(), Dis[i] = get_dis(Q[i].x, Q[i].y); int l = 0, r = 3e8, ans = 0; while (l <= r) { int mid = l + r >> 1; if (Check(mid)) r = mid - 1, ans = mid; else l = mid + 1; } cout<<ans<<endl; }
inline void Input () { N = read(), M = read(); for (int i = 1; i < N; ++i) { int x = read(), y = read(), z = read(); add_edge (x, y, z); add_edge (y, x, z); } }
int main() { #ifndef ONLINE_JUDGE freopen("transport.in", "r", stdin); freopen("transport.out", "w", stdout); #endif Input(); Solve(); return 0; }
|