LeetCode 1617. Count Subtrees With Max Distance Between Cities Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
1617. Count Subtrees With Max Distance Between Cities

Description

There are n cities numbered from 1 to n. You are given an array edges of size n-1, where edges[i] = [ui, vi] represents a bidirectional edge between cities ui and vi. 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 d from 1 to n-1, find the number of subtrees in which the maximum distance between any two cities in the subtree is equal to d.

Return an array of size n-1 where the dth element (1-indexed) is the number of subtrees in which the maximum distance between any two cities is equal to d.

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:

    <li><code>2 &lt;= n &lt;= 15</code></li>
    
    <li><code>edges.length == n-1</code></li>
    
    <li><code>edges[i].length == 2</code></li>
    
    <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li>
    
    <li>All pairs <code>(u<sub>i</sub>, v<sub>i</sub>)</code> are distinct.</li>
    

Solutions

Solution 1

PythonJavaC++GoTypeScript
class Solution: def countSubgraphsForEachDiameter( self, n: int, edges: List[List[int]] ) -> List[int]: def dfs(u: int, d: int = 0): nonlocal mx, nxt, msk if mx < d: mx, nxt = d, u msk ^= 1 << u for v in g[u]: if msk >> v & 1: dfs(v, d + 1) g = defaultdict(list) for u, v in edges: u, v = u - 1, v - 1 g[u].append(v) g[v].append(u) ans = [0] * (n - 1) nxt = mx = 0 for mask in range(1, 1 << n): if mask & (mask - 1) == 0: continue msk, mx = mask, 0 cur = msk.bit_length() - 1 dfs(cur) if msk == 0: msk, mx = mask, 0 dfs(nxt) ans[mx - 1] += 1 return ans(code-box)

Solution 2

PythonJavaC++GoTypeScript
class Solution: def countSubgraphsForEachDiameter( self, n: int, edges: List[List[int]] ) -> List[int]: def bfs(u: int) -> int: d = -1 q = deque([u]) nonlocal msk, nxt msk ^= 1 << u while q: d += 1 for _ in range(len(q)): nxt = u = q.popleft() for v in g[u]: if msk >> v & 1: msk ^= 1 << v q.append(v) return d g = defaultdict(list) for u, v in edges: u, v = u - 1, v - 1 g[u].append(v) g[v].append(u) ans = [0] * (n - 1) nxt = 0 for mask in range(1, 1 << n): if mask & (mask - 1) == 0: continue msk = mask cur = msk.bit_length() - 1 bfs(cur) if msk == 0: msk = mask mx = bfs(nxt) ans[mx - 1] += 1 return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !