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
| #include <bits/stdc++.h> using namespace std;
const int N = 1e6 + 66; int n, m, res, pre; int l1, r1, l2, r2, q1[N], q2[N]; int f[N], g[N][6];
inline void dfs1(int x) { int i, y, z, fuck; for (i = head[x]; i; i = nex[i]) { y = to[i], z = len[i]; dfs1(y); fuck = g[y][1] + z; if (fuck > g[x][1]) { g[x][2] = g[x][1]; g[x][1] = fuck; } if (fuck > g[x][2] && fuck < g[x][1]) g[x][2] = fuck; } return; }
inline void dfs2(int x) { int i, y, z, fuck; for (i = head[x]; i; i = nex[i]) { y = to[i], z = len[i]; fuck = g[x][1] == g[y][1] + z ? g[x][2] : g[x][1]; fuck = max(fuck, f[x]); fuck += z; f[y] = fuck; dfs2(y); } return; }
signed main() { int i, y, z; n = read(), m = read(); for (i = 2; i <= n; ++ i) { y = read(), z = read(); add(y, i, z); } f[1] = 0, dfs1(1), dfs2(1); for (i = 1; i <= n; ++ i) f[i] = max(f[i], g[i][1]);
l1 = l2 = 1; for (i = 1; i <= n; ++ i) { while (l1 <= r1 && f[i] < f[q1[r1]]) -- r1; q1[++ r1] = i; while (l2 <= r2 && f[i] > f[q2[r2]]) -- r2; q2[++ r2] = i; while (f[q2[l2]] - f[q1[l1]] > m) { if (q2[l2] < q1[l1]) pre = q2[l2], ++ l2; else pre = q1[l1], ++ l1; } res = max(res, i - pre); } put(res); return 0; }
|