A876 | Balancing a Tree--Gold
来源USACO
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
Farmer John has conducted an extensive study of the evolution of different cow
breeds. The result is a rooted tree with $N$ ($2\le N\le 10^5$) nodes labeled
$1\ldots N$, each node corresponding to a cow breed. For each $i\in [2,N]$,
the parent of node $i$ is node $p_i$ ($1\le p_i<i$), meaning that breed $i$
evolved from breed $p_i$. A node $j$ is called an ancestor of node $i$ if
$j=p_i$ or $j$ is an ancestor of $p_i$.
Every node $i$ in the tree is associated with a breed having an integer number
of spots $s_i$. The "imbalance" of the tree is defined to be the maximum of
$|s_i-s_j|$ over all pairs of nodes $(i,j)$ such that $j$ is an ancestor of
$i$.
Farmer John doesn't know the exact value of $s_i$ for each breed, but he knows
lower and upper bounds on these values. Your job is to assign an integer value
of $s_i \in [l_i,r_i]$ ($0\le l_i\le r_i\le 10^9$) to each node such that the
imbalance of the tree is minimized.
breeds. The result is a rooted tree with $N$ ($2\le N\le 10^5$) nodes labeled
$1\ldots N$, each node corresponding to a cow breed. For each $i\in [2,N]$,
the parent of node $i$ is node $p_i$ ($1\le p_i<i$), meaning that breed $i$
evolved from breed $p_i$. A node $j$ is called an ancestor of node $i$ if
$j=p_i$ or $j$ is an ancestor of $p_i$.
Every node $i$ in the tree is associated with a breed having an integer number
of spots $s_i$. The "imbalance" of the tree is defined to be the maximum of
$|s_i-s_j|$ over all pairs of nodes $(i,j)$ such that $j$ is an ancestor of
$i$.
Farmer John doesn't know the exact value of $s_i$ for each breed, but he knows
lower and upper bounds on these values. Your job is to assign an integer value
of $s_i \in [l_i,r_i]$ ($0\le l_i\le r_i\le 10^9$) to each node such that the
imbalance of the tree is minimized.
输入格式
The first line contains $T$ ($1\le T\le 10$), the number of independent test
cases to be solved, and an integer $B\in \\{0,1\\}$.
Each test case starts with a line containing $N$, followed by $N-1$ integers
$p_2,p_3,\ldots,p_N$.
The next $N$ lines each contain two integers $l_i$ and $r_i$.
It is guaranteed that the sum of $N$ over all test cases does not exceed
$10^5$.
cases to be solved, and an integer $B\in \\{0,1\\}$.
Each test case starts with a line containing $N$, followed by $N-1$ integers
$p_2,p_3,\ldots,p_N$.
The next $N$ lines each contain two integers $l_i$ and $r_i$.
It is guaranteed that the sum of $N$ over all test cases does not exceed
$10^5$.
输出格式
For each test case, output one or two lines, depending on the value of $B$.
The first line for each test case should contain the minimum imbalance.
If $B=1,$ then print an additional line with $N$ space-separated integers
$s_1,s_2,\ldots, s_N$ containing an assignment of spots that achieves the
above imbalance. Any valid assignment will be accepted.
The first line for each test case should contain the minimum imbalance.
If $B=1,$ then print an additional line with $N$ space-separated integers
$s_1,s_2,\ldots, s_N$ containing an assignment of spots that achieves the
above imbalance. Any valid assignment will be accepted.
输入输出样例
输入 #1
3 0 3 1 1 0 100 1 1 6 7 5 1 2 3 4 6 6 1 6 1 6 1 6 5 5 3 1 1 0 10 0 1 9 10
输出 #1
3 1 4
For the first test case, the minimum imbalance is $3$. One way to achieve
imbalance $3$ is to set $[s_1,s_2,s_3]=[4,1,7]$.
imbalance $3$ is to set $[s_1,s_2,s_3]=[4,1,7]$.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted