Class Solution
-
- All Implemented Interfaces:
public final class Solution1617 - Count Subtrees With Max Distance Between Cities\.
Hard
There are
ncities numbered from1ton. You are given an arrayedgesof sizen-1, where <code>edgesi = u<sub>i</sub>, v<sub>i</sub></code> represents a bidirectional edge between cities <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. There exists a unique path between each pair of cities. In other words, the cities form a tree.A subtree is a subset of cities where every city is reachable from every other city in the subset, where the path between each pair passes through only the cities from the subset. Two subtrees are different if there is a city in one subtree that is not present in the other.
For each
dfrom1ton-1, find the number of subtrees in which the maximum distance between any two cities in the subtree is equal tod.Return an array of size
n-1where the <code>d<sup>th</sup></code> element (1-indexed) is the number of subtrees in which the maximum distance between any two cities is equal tod.Notice that the distance between the two cities is the number of edges in the path between them.
Example 1:
Input: n = 4, edges = \[\[1,2],2,3,2,4]
Output: 3,4,0
Explanation:
The subtrees with subsets {1,2}, {2,3} and {2,4} have a max distance of 1.
The subtrees with subsets {1,2,3}, {1,2,4}, {2,3,4} and {1,2,3,4} have a max distance of 2.
No subtree has two nodes where the max distance between them is 3.
Example 2:
Input: n = 2, edges = \[\[1,2]]
Output: 1
Example 3:
Input: n = 3, edges = \[\[1,2],2,3]
Output: 2,1
Constraints:
2 <= n <= 15edges.length == n-1edges[i].length == 2<code>1 <= u<sub>i</sub>, v<sub>i</sub><= n</code>
All pairs <code>(u<sub>i</sub>, v<sub>i</sub>)</code> are distinct.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntArraycountSubgraphsForEachDiameter(Integer n, Array<IntArray> edges)-
-
Method Detail
-
countSubgraphsForEachDiameter
final IntArray countSubgraphsForEachDiameter(Integer n, Array<IntArray> edges)
-
-
-
-