【问题标题】:Distribute weight along the edges of a tree沿树的边缘分配权重
【发布时间】: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


【解决方案1】:

有两种情况:

  1. 如果每条边都参与至少一个直径,那么您能做的最好的事情就是在S - 1 条边中尽可能均匀地划分S。因此,答案是⌈S/(N-1)⌉。您的测试 #1 就是这种情况。
  2. 如果至少存在一个不参与任何直径的边,那么您可以将所有 S 分布在那里,答案是 0。这是测试 #2 中边 1-4 的情况.

因此,我们将问题简化为确定所有不属于任何直径的边。有一个写得很好的答案here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 2019-10-17
    • 1970-01-01
    • 2020-02-09
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多