#P9305. 「DTOI-5」校门外的枯树

    ID: 10065 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>模拟二分O2优化深度优先搜索 DFS

「DTOI-5」校门外的枯树

Background

Withered trees inside the school gate, 25/02/24.


One day after school, you walked out of the school gate and found that another row of trees had appeared outside. However, since it was the middle of winter, all the leaves had fallen off. The trees were trembling in the cold wind, making you worry that they might lose balance at some moment and then fall down.

Problem Description

You are given a row of TT undirected rooted trees outside the school gate (the root of each tree is node 11). Each edge of each tree has a weight mim_i. Now you need to compute the imbalance of each tree or the imbalance of the subtree of every node in that tree, so that the school can reinforce them. $\color{white}\sout{\text{Don’t ask me why the weight uses the letter }m\text{.}}$

Note that the edges of the tree are ordered, you can’t possibly break off a branch and graft it somewhere else, this is a withered tree after all.


For a rooted tree, define its imbalance as follows: choose a shortest path from the root to some leaf node, and use this path to split the tree’s edges into a left part and a right part (two edge sets). (Neither edge set contains any edge on the chosen shortest path.) The imbalance is the minimum possible value of the absolute difference between the total edge weights of the two parts.

In particular, the imbalance of a single node (a tree with one node) is 00. The total weight of an empty edge set is 00.

Input Format

The first line contains two positive integers T,kT, k, where TT is the number of test cases (i.e., the total number of trees). In each test case:

  • If k=1k = 1, you only need to compute the imbalance of the whole tree.
  • If k=2k = 2, you need to compute the imbalance of the subtree of each node separately.

For each tree, the first line contains a positive integer nn, the number of nodes in the tree.

The next nn lines describe the nodes. Line uu describes node uu:

  • The first positive integer is the number of children of this node, denoted by xx.
  • Then there are 2x2x positive integers. The (2i1)(2i-1)-th number is the ii-th child vv of node uu from left to right, and the (2i)(2i)-th number is the weight of the edge connecting uu and vv, denoted by muvm_{u\leftrightarrow v}.

Output Format

Output TT lines:

  • If k=1k = 1, line ii outputs one non-negative integer, the imbalance of the ii-th tree.
  • If k=2k = 2, line ii outputs nn non-negative integers; the jj-th number is the imbalance of the subtree rooted at node jj in the ii-th tree.
2 1
7
3 3 2 2 114 4 7
3 5 19 6 19 7 514
0
0
0
0
0

11
5 2 4 3 9 8 1 9 7 11 10
0
3 4 8 5 3 7 2
0
1 6 6
0
0
0
1 10 5
0
0
33
2
2 2
7
3 3 2 2 114 4 7
3 5 19 6 19 7 514
0
0
0
0
0

11
5 2 4 3 9 8 1 9 7 11 10
0
3 4 8 5 3 7 2
0
1 6 6
0
0
0
1 10 5
0
0
33 38 0 0 0 0 0
2 0 6 0 0 0 0 0 0 0 0

Hint

Constraints

No bundled testdata. Within the same Subtask\text{Subtask}, each test point is equally weighted.

$$%\def\or{\operatorname{or}} %this arraystretch is always misspelled/oh \def\arraystretch{1.5} \begin{array}{|c|c|c|c|c|}\hline \textbf{Subtask}&\sum n\leqslant&k=&\textbf{Special Constraints}&\textbf{Score}\cr\hline \sf1&2\times10^5&1&\bf A&1\cr\hline \sf2&20&1&T=1&5\cr\hline \sf3&5000&1&&5\cr\hline \sf4&10^5&2&\bf B&15\cr\hline \sf5&3\times10^5&1&&30\cr\hline \sf6&2\times10^5&2&\bf A'&4\cr\hline \sf7&3\times10^5&2&&40\cr\hline \end{array}$$
  • Special property A\bf A width limit 2.6 m: Each tree is guaranteed to have exactly one leaf node (n2n \geqslant 2).
  • Special property B\bf B height limit 4.5 m: Each tree is guaranteed to be a star (the root has n1n-1 children).
  • Special property A\bf A': Each tree is guaranteed to be a chain (the degree of each node is at most 22).

Difference between A\bf A and A\bf A': in A\bf A', the root may have degree 22, while in A\bf A, the root obviously has degree 11.

For 100%100\% of the data: T4000T \leqslant 4000, 1n105{\color{red}\textbf1}\leqslant n\leqslant 10^5, n3×105\sum n\leqslant 3\times10^5, 1mi1041 \leqslant m_i \leqslant 10^4, 1u,vn1 \leqslant u, v \leqslant n, k{1,2}k \in \{1, 2\}.


A leaf node is a node with no children, i.e., a node (except the root) whose degree in the tree is 11.

In the sample input, blank lines are added for readability; there are no blank lines in the actual data.

Explanation for Sample 11\bm1-\bm1

The tree is shown below, and the edge weights are the edge weights.

The optimal solution chooses 1271 \to 2 \to 7 as the splitting path, and the imbalance is (2+19+19)7=33{\large\vert}(2+19+19)-7{\large\vert}=33.

If you choose 1261 \to 2 \to 6 as the splitting path, then the difference between the total edge weights of the two parts is (2+19)(7+514)=500{\large\vert}(2+19)-(7+514){\large\vert}=500, which is not minimal.

$\color{transparent}\sout{Note that: \begin{cases}114+2+19+19=154\\114+514+19+19=666\end{cases}}$

Explanation for Sample 12\bm1-\bm2

The tree is shown below.

The optimal solution chooses 1371 \to 3 \to 7 as the splitting path, and the imbalance is (4+8+3+6)(1+7+5+10)=2{\large\vert}(4+8+3+6)-(1+7+5+10){\large\vert}=2

Translated by ChatGPT 5