AtCoder Regular Contest 160
E
Make Biconnected
AtCoder
⌀ 3284
原题
← DF →
翻译中…

Problem Statement

翻译中…

You are given an undirected tree $G$ with $N$ vertices. The degree of every vertex in $G$ is at most $3$. The vertices are numbered $1$ to $N$. The edges are numbered $1$ to $N-1$, and edge $i$ connects vertex $u_i$ and vertex $v_i$. Each vertex has a fixed weight, and the weight of vertex $i$ is $W_i$.

翻译中…

You will add zero or more edges to $G$. The cost of adding an edge between vertex $i$ and vertex $j$ is $W_i + W_j$.

翻译中…

Print one way to add edges to satisfy the following condition for the minimum possible total cost.

翻译中…
  • $G$ is $2$-vertex-connected. In other words, for every vertex $v$ in $G$, removing $v$ and its incident edges from $G$ would not disconnect $G$.
翻译中…

You have $T$ test cases to solve.

翻译中…

Constraints

翻译中…
  • $1 \leq T \leq 2 \times 10^5$
  • $3 \leq N \leq 2 \times 10^5$
  • $1 \leq u_i, v_i \leq N$
  • The given graph is a tree.
  • The degree of every vertex in the given graph is at most $3$.
  • $1 \leq W_i \leq 10^9$
  • $W_i$ is an integer.
  • The sum of $N$ across the test cases is at most $2 \times 10^5$.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format, where $\mathrm{case}_i$ represents the $i$-th test case:

翻译中…

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_N

翻译中…

Each test case is in the following format:

翻译中…

N
W_1 W_2 \dots W_N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

翻译中…

Output

翻译中…

For each test case, print a solution in the following format, where:

翻译中…
  • $M$ is the number of added edges, and
  • the $i$-th added edge connects vertex $a_i$ and vertex $b_i$.
翻译中…

If multiple solutions exist, any of them would be accepted.

翻译中…

M
a_1 b_1
a_2 b_2
\vdots
a_M b_M

翻译中…

样例

Input #1
2
3
2 3 5
1 2
2 3
7
1 10 100 1000 10000 100000 1000000
1 2
2 3
2 4
3 5
3 6
4 7
Output #1
1
1 3
2
7 6
1 5
In the first test case, adding an edge connecting vertex $1$ and vertex $3$ makes $G$ satisfy the condition in the problem statement. The cost of this is $W_1 + W_3 = 2 + 5 = 7$. There is no way to add edges to satisfy the condition for a cost of less than $7$, so this is a valid solution. In the second test case, the solution above has a total cost of $(W_7 + W_6) + (W_1 + W_5) = 1100000 + 10001 = 1110001$, which is the minimum possible.
题目信息
题号
arc160_e
时间限制
3 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。