#P8934. [JRKSJ R7] TSM eerT

    ID: 9939 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>模拟2023洛谷原创O2优化树的直径

[JRKSJ R7] TSM eerT

Problem Description

For a weighted tree TT with nn nodes, define dis(x,y)dis(x,y) as the sum of edge weights on the path xyx\to y in TT. Then define an undirected complete graph p(T)=Gp(T)=G with nn nodes, where for all x,y[1,n]x,y\in [1,n], the weight of edge (x,y)(x,y) in GG is dis(x,y)dis(x,y).

Define f(T)f(T) as the maximum spanning tree of p(T)p(T). In particular, if the maximum spanning tree of p(T)p(T) is not unique, you must determine it immediately and report it.

Given a tree T0T_0 and an integer kk, find fk(T0)f^k(T_0). Its definition is given below.

Input Format

The first line contains two integers n,kn,k.
In the next lines 2n2\sim n, line ii contains two integers ifi,vii-f_i,v_i, representing an edge (i,fi)(i,f_i) of T0T_0 with weight viv_i. That is, this line inputs two integers fi,vif'_i,v_i, and the real fi=ifif_i=i-f'_i.

Output Format

Output only one integer as the answer.

If x[0,k1]\exists x\in[0,k-1] such that the maximum spanning tree of p(fx(T0))p(f^x(T_0)) is not unique, output 1-1. Otherwise, output the sum of all edge weights of fk(T0)f^k(T_0) modulo 2322^{32}.

3 3
1 1
2 2
13
10 2
1 7
1 2
1 5
4 5
2 1
3 9
2 9
4 4
9 4
736
4 1
1 1
2 1
3 1
-1

Hint

Definition

The definition of fk(T)f^k(T) is:

$$f^k(T)=\begin{cases}T&k=0\\f(f^{k-1}(T))&k>0\end{cases}$$

Explanation for Sample 11

They are T0,f(T0),f2(T0),f3(T0)T_0,f(T_0),f^2(T_0),f^3(T_0).

Take the process of computing f(T0)f(T_0) as an example. The generated p(T0)=Gp(T_0)=G is

The edges in the maximum spanning tree are (1,3),(2,3)(1,3),(2,3).

Constraints

This problem uses bundled testdata.

Subtask\text{Subtask} nn\le kk\le Score\text{Score}
11 10310^3 11 1010
22 10510^5 2020
33 10610^6 3030
44 10710^7 4040

For 100%100\% of the data, 2n1062\le n\le 10^6, 1k1071\le k\le 10^7, 1fi<i1\le f_i<i, 1vi1091\le v_i\le10^9.

Special Scoring Method

This problem enables subtask dependency, as follows:

  • For subtask ii, you need to solve all subtasks j[1,i]j\in[1,i] to get the score of subtask ii.

Translated by ChatGPT 5