Min-Max Weighted Edge
Practice
3.9 (10 votes)
Algorithms
Breadth first search
Depth first search
Graphs
Medium
Sets
Trees
Problem
26% Success 4290 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Given 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 path is the minimum possible

You have to output this minimum possible edge weight.

Note: The diameter of a tree is the number of nodes on the longest path between two leaves in the tree.

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\).

Output Format

For each test case output the minimum possible edge weight which satisfies the above-mentioned conditions.

Constraints

\(1 \le T \le 10\)
\(1 \le N \le 2 \times 10^3\)
\(1 \le u, v \le N\)
\(1 \le S \le 10^9\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
4 votes
Tags:
AlgorithmsDepth First SearchDijkstra's algorithmDynamic ProgrammingGraphsMedium
Points:30
10 votes
Tags:
Ad-HocAlgorithmsCombinatoricsGraphsMedium普通
Points:30
4 votes
Tags:
AlgorithmsDijkstra's algorithmGraphsMedium