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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| #include<bits/stdc++.h> using namespace std;
inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'|| ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;} #define Z(x) (x)*(x) #define pb push_back
#define N 500010
struct node { int x; long long y, z; }; int n, m, i, j, k, T, p1, p2, in[N]; int u, v, w, qe; vector<node>G[N];
struct Tree { int i, j, k, rt, mn[N]; long long h[N], mxh[N], mx[N], sum[N]; int son[N], dep[N], top[N]; int f[N][22], rk[N], dfn[N]; node w[N]; void dfs1(int x, int fa, int &p1) { if(h[x]>h[p1]) p1=x; for(auto t : G[x]) { int y=t.y; long long z=t.z; if(y==fa) continue; h[y]=h[x]+z; dfs1(y, x, p1); } } void dfs2(int x, int fa) { dep[x]=dep[fa]+1; mx[x]=mxh[x]=h[x]; for(auto t : G[x]) { int y=t.y; long long z=t.z; if(y==fa) continue; h[y]=h[x]+z;
dfs2(y, x); mx[x]=max(mx[x], mx[y]); if(mxh[y]>mxh[son[x]]) son[x]=y; } if(son[x]) mxh[x]=mxh[son[x]]; } void dfs3(int x, int fa, int tp) {
top[x]=tp; f[x][0]=fa; if(in[x]==1 && fa) { w[x].y=h[x]-h[f[top[x]][0]]; w[x].x=x; } for(auto t : G[x]) { int y=t.y; if(y==fa) continue; if(y==son[x]) dfs3(y, x, tp); else dfs3(y, x, y); } } void init() {
sort(w+1, w+n+1, [] (node x, node y) { return x.y<y.y; }) ; reverse(w+1, w+n+1); for(i=1; i<=n; ++i) {
if(w[i].x) sum[i]=w[i].y, rk[w[i].x]=i, dfn[i]=w[i].x; sum[i]+=sum[i-1]; }
for(k=1; k<=19; ++k) for(i=1; i<=n; ++i) f[i][k]=f[f[i][k-1]][k-1]; } void dfs4(int x, int fa) { if(in[x]==1 && fa) mn[x]=rk[x]; else mn[x]=1e9; for(auto t : G[x]) { int y=t.y, z=t.z; if(y==fa) continue; dfs4(y, x); mn[x]=min(mn[x], mn[y]); } } int tiao(int x, int g) { for(k=19; k>=0; --k) if(mn[f[x][k]]>g) x=f[x][k]; return f[x][0]; } int lca(int x, int y) { if(x==y) return x; if(dep[x]<dep[y]) swap(x, y); for(int k=19; k>=0; --k) if(dep[f[x][k]]>=dep[y]) x=f[x][k]; if(x==y) return x; for(int k=19; k>=0; --k) if(f[x][k]!=f[y][k]) x=f[x][k], y=f[y][k]; return f[x][0]; } long long calc(int y, int oldy, int newx) {
return min(w[mn[y]].y, h[oldy]-h[y]); } long long que(int x, int k) { if(k==1) {
return mx[x]; } if(mn[x]<=2*k-1) { return sum[min(2*k-1, n)]; } int y=tiao(x, 2*k-1), newx, oldy; long long ans; newx=dfn[mn[x]]; oldy=dfn[mn[y]];
ans=sum[2*k-1]-calc(y, oldy, newx)+(h[newx]-h[y]); ans=max(ans, sum[2*k-1]-w[2*k-1].y+(h[newx]-h[y])); return ans; } }T1, T2;
void print(long long x) { if(x) print(x/10), putchar(x%10+'0'); }
signed main() {
freopen("bomb.in", "r", stdin); freopen("bomb.out", "w", stdout);
n=read(); qe=read(); for(i=1; i<n; ++i) { u=read(); v=read(); w=read(); G[u].pb({u, v, w}); G[v].pb({v, u, w}); ++in[u]; ++in[v]; } T1.h[1]=0; T1.dfs1(1, 0, p1); T1.h[p1]=0; T1.dfs1(p1, 0, p2); T1.rt=p1; T2.rt=p2; T1.h[p1]=0; T1.dfs2(p1, 0); T2.h[p2]=0; T2.dfs2(p2, 0);
T1.dfs3(p1, 0, p1); T2.dfs3(p2, 0, p2); T1.init(); T2.init(); T1.dfs4(p1, 0); T2.dfs4(p2, 0); while(qe--) { u=read(); k=read(); print(max(T1.que(u, k), T2.que(u, k))); puts(""); } return 0; }
|