#P8934. Iterated Maximum Spanning Tree on Tree Metrics
Iterated Maximum Spanning Tree on Tree Metrics
Iterated Maximum Spanning Tree on Tree Metrics
Given a weighted tree (T) with (n) nodes (numbered from 1 to (n)) and (n-1) edges, define for any two nodes (x,y) the distance (dis_T(x,y)) as the sum of edge weights on the unique simple path from (x) to (y) in (T). Using these distances, construct a complete undirected graph (G=p(T)) on (n) nodes where every edge ((x,y)) has weight (dis_T(x,y)). Then, define (f(T)) as the maximum spanning tree (i.e. a spanning tree with the maximum total weight) of (G).
It is possible that (G) has more than one maximum spanning tree. In that case, you must immediately detect this ambiguity and report it.
Now, for a given initial tree (T_0) and an integer (k), define (f^k(T_0)) as the tree obtained by applying the function (f(\cdot)) repeatedly (k) times; that is, (T_1=f(T_0)), (T_2=f(T_1)), and so on. Your task is to compute (f^k(T_0)).
Since a tree is uniquely determined by its (n-1) edges, if all the intermediate applications of (f(\cdot)) produce a unique maximum spanning tree, output the sum of the weights of the edges in (f^k(T_0)). Otherwise, if at any iteration the maximum spanning tree is not unique, print Not Unique
immediately and do not continue further.
inputFormat
The first line contains two integers (n) and (k) (number of nodes and the number of iterations).
Each of the next (n-1) lines contains three integers (u), (v) and (w), denoting an edge between nodes (u) and (v) with weight (w).
It is guaranteed that the given edges form a tree.
outputFormat
If at any iteration the maximum spanning tree of (p(T)) is not unique, output exactly Not Unique
(without quotes). Otherwise, after (k) iterations, output a single integer representing the sum of the edge weights of the tree (f^k(T_0)).
sample
3 1
1 2 3
2 3 2
8