题目收集
题目列表题单
返回列表

Turing Daughter

CodeforcesP0076· 8000ms· 1024 MB· ZJU Summer 2026 Contest 7原题链接
数据结构优化分块LCT

创建于 2026/07/18 · 更新于 2026/07/18

题目描述

Turing Daughter is Turing Grandma's granddaughter. Like her grandmother, she also loves knitting wool sweaters.

Each sweater she knits is represented by an unweighted tree with nnn vertices numbered 1,2,…,n1,2,\ldots,n1,2,…,n. Its n−1n-1n−1 edges have pairwise distinct colors numbered 1,2,…,n−11,2,\ldots,n-11,2,…,n−1.

She first knits a sweater T0T_0T0​. She then modifies it n−1n-1n−1 times in order, producing the sweaters T1,T2,…,Tn−1.T_1,T_2,\ldots,T_{n-1}.T1​,T2​,…,Tn−1​.

In the iii-th modification, she removes the edge of color eie_iei​ from Ti−1T_{i-1}Ti−1​ and knits that edge back between vertices uiu_iui​ and viv_ivi​, keeping its color eie_iei​. It is guaranteed that the resulting sweater is still represented by a tree.

Therefore, there are exactly nnn sweaters: T0,T1,…,Tn−1T_0,T_1,\ldots,T_{n-1}T0​,T1​,…,Tn−1​.

The complexity of a sweater is the diameter of its tree: the greatest number of edges on a simple path between two of its vertices. In particular, a sweater consisting of a single vertex has complexity 000.

For a sweater TTT, removing its edge of color xxx splits it into two smaller sweaters, whose trees are the two connected components C1C_1C1​ and C2C_2C2​. Define f(T,x)=max⁡(diameter⁡(C1),diameter⁡(C2)).f(T,x)=\max\bigl(\operatorname{diameter}(C_1),\operatorname{diameter}(C_2)\bigr).f(T,x)=max(diameter(C1​),diameter(C2​)).

There are qqq queries. In each query, Turing Daughter chooses a time interval [l,r][l,r][l,r] and removes the edge of color xxx from every sweater Tl,Tl+1,…,TrT_l,T_{l+1},\ldots,T_rTl​,Tl+1​,…,Tr​. This leaves exactly 2(r−l+1)2(r-l+1)2(r−l+1) smaller sweaters. She wants to know the maximum complexity among all of them, that is, max⁡i=lrf(Ti,x).\max_{i=l}^{r} f(T_i,x).maxi=lr​f(Ti​,x).

输入格式

The first line contains two integers nnn and qqq (2≤n≤5×1042\le n\le 5\times 10^42≤n≤5×104, 1≤q≤5×1041\le q\le 5\times 10^41≤q≤5×104).

The next n−1n-1n−1 lines describe T0T_0T0​. The eee-th of these lines contains two integers ueu_eue​ and vev_eve​ (1≤ue,ve≤n1\le u_e,v_e\le n1≤ue​,ve​≤n), indicating that the edge of color eee connects vertices ueu_eue​ and vev_eve​.

The next n−1n-1n−1 lines describe the modifications. The iii-th of these lines contains three integers ei,ui,vie_i,u_i,v_iei​,ui​,vi​ (1≤ei<n1\le e_i \lt n1≤ei​<n, 1≤ui,vi≤n1\le u_i,v_i\le n1≤ui​,vi​≤n), describing the modification that transforms Ti−1T_{i-1}Ti−1​ into TiT_iTi​.

The next qqq lines each contain three integers l,r,xl,r,xl,r,x (0≤l≤r<n0\le l\le r \lt n0≤l≤r<n, 1≤x<n1\le x \lt n1≤x<n), describing a query.

It is guaranteed that T0T_0T0​ and the graph obtained after every modification are trees.

输出格式

For each query, output one integer on a separate line.

样例输入 #1

4 4
1 2
2 3
3 4
1 1 2
2 1 3
3 2 4
0 0 2
0 3 2
1 2 1
2 3 3

样例输出 #1

1
2
2
2