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
| #include <bits/stdc++.h> #define MAX_N 100000 #define mid ((l+r)>>1) using namespace std; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } int n, m, L, R, rt, cnt, ind, tot, f[MAX_N+5], g[MAX_N+5]; int pr[MAX_N+5], sz[MAX_N+5], w[MAX_N+5], dfn[MAX_N+5]; int val[MAX_N+5], dep[MAX_N+5], d[MAX_N<<1], ord[MAX_N+5]; bool mrk[MAX_N+5], vis[MAX_N+5]; queue <int> que, bin; struct edge {int v, c, w, nxt;} E[MAX_N<<1]; bool cmp(const int &x, const int &y) {return d[x] < d[y];} void insert(int u, int v, int c) {E[cnt] = (edge){v, c, c, pr[u]}, pr[u] = cnt++;} void addedge(int u, int v, int c) {insert(u, v, c), insert(v, u, c);} void getrt(int u, int fa) { sz[u] = 1, w[u] = 0; for (int i = pr[u], v; ~i; i = E[i].nxt) if (((v = E[i].v) ^ fa) && !mrk[v]) getrt(v, u), sz[u] += sz[v], w[u] = max(w[u], sz[v]); if ((w[u] = max(w[u], tot-sz[u])) < w[rt]) rt = u; } void init(int u) { int num = 0; dfn[++ind] = u, mrk[u] = true; for (int i = pr[u], v, mxd = 0; ~i; i = E[i].nxt, mxd = 0) if (!mrk[v = E[i].v]) { while (!que.empty()) que.pop(); memset(vis, false, sizeof vis); que.push(v), dep[v] = 1, vis[v] = true; while (!que.empty()) { int p = que.front(); que.pop(); if (dep[p] >= R) continue; mxd = max(mxd, dep[p]); for (int j = pr[p], q; ~j; j = E[j].nxt) if (!mrk[q = E[j].v] && !vis[q]) que.push(q), dep[q] = dep[p]+1, vis[q] = true; } d[ord[++num] = i] = mxd; } else d[ord[++num] = i] = n; sort(ord+1, ord+num+1, cmp), pr[u] = ord[1]; for (int i = 1; i < num; i++) E[ord[i]].nxt = ord[i+1]; E[ord[num]].nxt = -1; for (int i = pr[u], v; ~i; i = E[i].nxt) if (sz[u] < sz[v = E[i].v]) sz[v] = tot-sz[u]; for (int i = pr[u], v; ~i; i = E[i].nxt) if (!mrk[v = E[i].v] && sz[v] > L) w[rt = 0] = tot = sz[v], getrt(v, u), init(rt); } bool DFS(int stp) { if (stp > ind) return false; int u = dfn[stp], mxd = 0; f[0] = 0, mrk[u] = true; for (int i = pr[u], v; ~i; i = E[i].nxt) if (!mrk[v = E[i].v]) { int s = 1, t = 0; while (!que.empty()) que.pop(); while (!bin.empty()) bin.pop(); memset(vis, false, sizeof vis); for (int j = mxd; j >= L; d[++t] = j--) while (s <= t && f[d[t]] <= f[j]) t--; que.push(v), dep[v] = 1, g[v] = E[i].c, vis[v] = true; while (!que.empty()) { int p = que.front(); que.pop(), bin.push(p); while (s <= t && d[s]+dep[p] > R) s++; if (dep[p] <= L) { while (s <= t && f[d[t]] <= f[L-dep[p]]) t--; d[++t] = L-dep[p]; } if (s <= t && f[d[s]]+g[p] >= 0) return true; if (dep[p] >= R) continue; mxd = max(mxd, dep[p]); for (int j = pr[p], q; ~j; j = E[j].nxt) if (!mrk[q = E[j].v] && !vis[q]) que.push(q), dep[q] = dep[p]+1, g[q] = g[p]+E[j].c, vis[q] = true; } for (int x; !bin.empty(); bin.pop()) x = bin.front(), f[dep[x]] = max(f[dep[x]], g[x]); } for (int i = 0; i <= mxd; i++) f[i] = -n; return DFS(stp+1); } bool chk(int tans) { for (int i = 0; i < cnt; i++) E[i].c = E[i].w < tans ? -1 : 1; for (int i = 0; i <= n; i++) f[i] = -n; memset(mrk, false, sizeof mrk); return DFS(1); } int bi_search(int l, int r) { int ret = -1; while (l <= r) if (!chk(val[mid])) r = mid-1; else ret = val[mid], l = mid+1; return ret; } int main() { read(n), read(L), read(R), memset(pr, -1, sizeof pr); for (int i = 1, u, v, c; i < n; i++) read(u), read(v), read(c), addedge(u, v, c), val[i] = c; w[rt = 0] = tot = n, getrt(1, 0), init(rt); sort(val+1, val+n), m = (int)(unique(val+1, val+n)-val-1); return printf("%d\n", bi_search(1, m)), 0; }
|