Awesome Shawarma
Time limit14sMemory limit512 MB
Given a tree, count the unordered pairs of nodes whose added edge leaves the number of bridges in [L, R].
- Level
Hard8 of 10
- Topics
- Tree, DFS, Prefix sum, Combinatorics
- Solved
- No attempts yet
Problem
Fouad has a raw awesome shawarma, and he is in a city which is represented as an undirected tree. He heard that there is a magical oven that will cook the shawarma, to make it so delicious. However, in order to acquire the magical oven, two conditions must be satisfied in this city:
- One extra edge must be added to join two different nodes in the tree. (It is allowed to join two nodes that were previously connected by a direct edge.)
- The number of bridges after adding the new edge must be between [L, R] inclusively.
Help Fouad acquire the magical oven to cook the awesome shawarma by counting in how many ways he can add an edge that satisfies the conditions above.
Input
The first line of the input file contains a single integer T, the number of test cases.
Each test case begins with a line containing three integers N, L, and R (2 ≤ N ≤ 10^5, 0 ≤ L ≤ R ≤ N − 1), in which N is the number of nodes, and L and R are the minimum and maximum numbers of allowed bridges, respectively.
Then N − 1 lines follow, each line contains two integers Xi and Yi (1 ≤ Xi, Yi ≤ N), giving an edge between nodes Xi and Yi.
Output
For each test case, print a single line containing the number of ways to add one new edge such that the number of bridges in the new graph is within the range [L, R] inclusively.
Hint
A bridge in a graph is an edge such that if it is removed, the graph will become disconnected.
In the second sample, the result is acquired by connecting an edge between any two different nodes.