| 242 | } |
| 243 | |
| 244 | public static long calc(int u) { |
| 245 | cnte = 0; |
| 246 | for (int e = head[u]; e > 0; e = nxt[e]) { |
| 247 | int v = to[e]; |
| 248 | int c = color[e]; |
| 249 | if (!vis[v]) { |
| 250 | edgeArr[++cnte][0] = v; |
| 251 | edgeArr[cnte][1] = c; |
| 252 | } |
| 253 | } |
| 254 | Arrays.sort(edgeArr, 1, cnte + 1, (a, b) -> a[1] - b[1]); |
| 255 | update(preTree, 0, 0, 0, n, 1); |
| 256 | long ans = -INF; |
| 257 | cntc = 0; |
| 258 | for (int k = 1; k <= cnte; k++) { |
| 259 | int v = edgeArr[k][0]; |
| 260 | int c = edgeArr[k][1]; |
| 261 | if (k > 1 && edgeArr[k - 1][1] != c) { |
| 262 | clear(curTree, 0, n, 1); |
| 263 | for (int i = 1; i <= cntc; i++) { |
| 264 | int node = colorNode[i]; |
| 265 | update(preTree, edgeCnt[node], pathSum[node], 0, n, 1); |
| 266 | } |
| 267 | cntc = 0; |
| 268 | } |
| 269 | cnts = 0; |
| 270 | // dfs1(v, u, c, 1, val[c]); |
| 271 | dfs2(v, u, c, 1, val[c]); |
| 272 | for (int i = 1; i <= cnts; i++) { |
| 273 | int node = subtreeNode[i]; |
| 274 | int l = Math.max(0, limitl - edgeCnt[node]); |
| 275 | int r = limitr - edgeCnt[node]; |
| 276 | ans = Math.max(ans, query(preTree, l, r, 0, n, 1) + pathSum[node]); |
| 277 | ans = Math.max(ans, query(curTree, l, r, 0, n, 1) + pathSum[node] - val[c]); |
| 278 | } |
| 279 | for (int i = 1; i <= cnts; i++) { |
| 280 | int node = subtreeNode[i]; |
| 281 | colorNode[++cntc] = node; |
| 282 | update(curTree, edgeCnt[node], pathSum[node], 0, n, 1); |
| 283 | } |
| 284 | } |
| 285 | clear(preTree, 0, n, 1); |
| 286 | clear(curTree, 0, n, 1); |
| 287 | return ans; |
| 288 | } |
| 289 | |
| 290 | public static long solve(int u) { |
| 291 | vis[u] = true; |