#P7895. Interactive Tree Weight Sum
Interactive Tree Weight Sum
Interactive Tree Weight Sum
This is an interactive problem.
You are given a weighted tree with \( n \) nodes numbered from \( 1 \) to \( n \). The degree of each node is known. The goal is to compute the sum of the weights of all the edges in the tree.
You are allowed the following three types of operations on the tree:
- For any node present in the tree, query its DFS order. Here the DFS order is defined as the order in which the nodes are first visited when performing a depth-first search starting from node \( 1 \). Formally, if \( d(\cdot) \) denotes the DFS order, then the query returns \( d(i) \) for node \( i \). Note that once a deletion operation is performed the DFS order is recomputed.
- For any pair of nodes currently in the tree, query the distance between them, which is defined as the sum of the weights along the unique path connecting the two nodes. (In particular, the distance between a node and itself is \( 0 \)).
- Remove all nodes with degree \( 1 \) (i.e. the leaf nodes) from the current tree along with their incident edges. After a deletion, the remaining nodes are re-numbered so that their labels become \( 1, 2, \ldots, k \), where \( k \) is the number of nodes remaining.
You are allowed to make no more than 142 operations total (including the final answer submission). Your final task is to output the sum of the edge weights of the current tree before it becomes empty.
Note:
- \( \text{DFS order}\)^1: The DFS order is defined as above. The DFS order for a given tree is not unique. However, if no deletion is performed between two queries for the same node, the result of the DFS order query is guaranteed to be the same.
- \( \text{Distance}\)^2: The distance between two nodes is the sum of weights along the unique path between them. For a node with itself, the distance is 0.
Simplification for this problem: In this version, the interactive operations are simulated. You will be given the complete tree as input. Your task is to simply compute and output the sum of the weights of all the edges in the tree. This submission will count as one operation (i.e. the final answer submission) and will pass all test cases as long as it outputs the correct sum.
inputFormat
The input is given as follows:
n u1 v1 w1 u2 v2 w2 ... u_{n-1} v_{n-1} w_{n-1}
where an integer n
(\( 2 \leq n \leq 10^5 \)) represents the number of nodes in the tree, and the next \( n-1 \) lines each contain three integers \( u_i \), \( v_i \) (the endpoints of the edge) and \( w_i \) (the weight of the edge).
outputFormat
Output a single integer, which is the sum of the weights of all the edges in the tree.
sample
2
1 2 10
10
</p>