Count Subtrees With Max Distance Between Cities - Problem

Imagine you have n cities connected by roads forming a tree structure, where every city can reach every other city through a unique path. Your task is to analyze all possible connected subsets of cities and count how many have specific maximum distances.

You're given an array edges where edges[i] = [ui, vi] represents a bidirectional road between cities ui and vi. Since the cities form a tree, there are exactly n-1 edges.

A subtree is a subset of cities where:

  • Every city in the subset can reach every other city in the subset
  • The path between any two cities passes only through cities in the subset

Goal: For each distance d from 1 to n-1, count how many subtrees have a maximum distance of exactly d between any two cities in that subtree.

Return an array where the d-th element (1-indexed) represents the count of subtrees with maximum distance d.

Input & Output

example_1.py โ€” Basic Tree
$ Input: n = 4, edges = [[1,2],[2,3],[2,4]]
โ€บ Output: [3, 2, 1]
๐Ÿ’ก Note: Distance 1: {1,2}, {2,3}, {2,4} (3 subtrees). Distance 2: {1,2,3}, {1,2,4} (2 subtrees). Distance 3: {1,2,3,4} (1 subtree).
example_2.py โ€” Linear Tree
$ Input: n = 2, edges = [[1,2]]
โ€บ Output: [1]
๐Ÿ’ก Note: Only one possible subtree with 2 cities: {1,2} with maximum distance 1.
example_3.py โ€” Larger Tree
$ Input: n = 3, edges = [[1,2],[2,3]]
โ€บ Output: [2, 1]
๐Ÿ’ก Note: Distance 1: {1,2}, {2,3} (2 subtrees). Distance 2: {1,2,3} (1 subtree).

Visualization

Tap to expand
12345Subset {1,2,3,4}Distance AnalysisMaximum path: 1โ†’2โ†’4 (length=2)Or: 3โ†’2โ†’4 (length=2)This subset contributes to result[1](1-indexed, so distance 2 โ†’ index 1)Bitmask: 01111Bit 0 (island 1): 1 โœ“Bit 1 (island 2): 1 โœ“Bit 2 (island 3): 1 โœ“Bit 3 (island 4): 1 โœ“ Bit 4: 0
Understanding the Visualization
1
Tree Structure
Cities are connected as islands with bridges forming a tree - no cycles, unique paths
2
Subset Generation
Use binary masks to represent all possible island groups (2^n possibilities)
3
Validation
Check if island group is connected and count bridges within the group
4
Distance Calculation
Find the longest bridge path within each valid island group
5
Counting
Group results by maximum distance and return counts
Key Takeaway
๐ŸŽฏ Key Insight: By representing subsets as bitmasks and using tree diameter properties, we can efficiently check all 2^n possible island groups and calculate their maximum distances in O(nยท2^n) time instead of the naive O(nยท4^n).

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * 2^n)

2^n subsets, each requiring O(n) operations for validation and diameter calculation

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Adjacency list storage and BFS temporary arrays

n
2n
โšก Linearithmic Space

Constraints

  • 2 โ‰ค n โ‰ค 15
  • edges.length == n - 1
  • edges[i].length == 2
  • 1 โ‰ค ui, vi โ‰ค n
  • The given input represents a valid tree
Asked in
Google 25 Microsoft 18 Amazon 12 Meta 8
23.6K Views
Medium Frequency
~35 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen