#P15842. [Bulgarian NOI 2024] 最长路径 / longest

[Bulgarian NOI 2024] 最长路径 / longest

Problem Description

You are given a weighted tree with nn vertices. Its edges are (u1,v1)(u_1, v_1) (weight w1w_1), (u2,v2)(u_2, v_2) (weight w2w_2), …\dots, (un−1,vn−1)(u_{n-1}, v_{n-1}) (weight wn−1w_{n-1}). Write a program to support qq queries, where each query is one of the following two types:

  • Type 1: Given x,k,n1,…,nkx, k, n_1, \dots, n_k, find the maximum path length from vertex xx to any vertex yy, under the constraint that the path from xx to yy must not pass through any of the specified vertices n1,…,nkn_1, \dots, n_k.
  • Type 2: Given i,wi, w, modify the weight of the ii-th edge (ui,vi)(u_i, v_i) to ww.

Input Format

Read an integer nn from the first line of standard input. The next n−1n - 1 lines each contain three integers ui,vi,wiu_i, v_i, w_i, describing an edge connecting vertices uiu_i and viv_i with weight wiw_i. The next line contains an integer qq. Each of the following qq lines first gives the query type (1 or 2):

  • If the type is 1, then read xx, kk, and kk integers n1,…,nkn_1, \dots, n_k.
  • If the type is 2, then read ii and ww.

Output Format

For each type 1 query, print the required maximum path length on a separate line to standard output.

5
1 2 1
1 4 2
2 3 3
2 5 4
10
1 1 0
1 2 0
1 3 0
1 4 0
1 5 0
1 1 1 5
1 1 1 2
2 1 100
1 2 0
1 2 4 1 3 4 5
5
4
7
7
7
4
2
102
0

Hint

Sample 1 Explanation

In the queries, the target vertices that need to be found are, in order, 5,5,5,3,3,4,4,25, 5, 5, 3, 3, 4, 4, 2.

Constraints

  • 1≤n,q, ∑k≤2000001 \le n, q,\ \sum k \le 200000
  • 1≤ui,vi≤n1 \le u_i, v_i \le n
  • 1≤w,wi≤1091 \le w, w_i \le 10^9
  • For all 1≤i≤k1 \le i \le k, ni≠xn_i \ne x
  • For all 1≤i≠j≤k1 \le i \ne j \le k, ni≠njn_i \ne n_j

Subtasks

Subtask Points Additional Constraints
11 55 n,q≤5000n, q \le 5000
22 1515 The degree of each vertex is at most 22
33 k=0k = 0
44 3030 There are no type 22 queries
55 3535 None

You can obtain the points for a subtask only if you pass all testdata for that subtask.

Translated by Qwen3.5-397B-A17B.

Translated by ChatGPT 5