【发布时间】:2019-01-06 11:37:03
【问题描述】:
A tree with N nodes and N-1 Bidirectional edges, and given an integer S.
Now, you have to assign the weights to the edges of this tree such that:
1. the sum of the weights of all the edges is equal to S
2. for every possible diameter of the tree, the maximum weight over all the edges covered by its paths is the minimum possible.
Output this minimum possible edge weight.
The diameter of a tree is the number of nodes on the longest path between two leaves in the tree
Constraints
1 <= T <= 10
1 <= N <= 2*10^3
1 <= u,v <= N
1 <= S <= 10^9
Input Format
The first line of the input contains an Integer T, the total number of test cases.
The first line of each test case contains two space separated integers N and
S,the number of nodes in a tree and the integer S as mentioned in the problem statement.
Then the N-1 lines follow, each containing two space-separated integers u
and v representing that there is a bidirectional edge between the nodes u and v.
我无法制定策略以按要求的方式将权重分配到所有边缘。非常感谢任何帮助。
【问题讨论】:
-
根据问题描述,示例 1 给出的答案似乎不正确,因此很难知道您在问什么。
-
@MattTimmermans 的答案是正确的,例如 1 存在 6 个直径(4-> 7, 4-> 8, 5-> 7, 5-> 8, 6-> 7, 6-> 8) 长度相等,其中总边缘重量最低的直径为 4->8,根据问题,我们必须从总边缘重量最低的直径返回重量最大的边缘。因此,在 4->8 中权重最大的边是 3,因此答案是 3。我要问的是一种方法,我将在树中的边上分配权重,同时牢记 2 个条件。
-
哦,我明白了...“所有边缘的最大重量”不是一个总和。
标签: algorithm data-structures tree time-complexity computer-science