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 sizen-1where thedth ___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.
****
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 is3.
Since n is small (≤ 15), we can enumerate all possible subsets of cities (using bitmasking). For each subset, if it forms a connected subtree, we can compute the maximum distance between any two cities in the subset using BFS. We count the number of subtrees for each possible maximum distance.
classSolution {
funcountSubgraphsForEachDiameter(n: Int, edges: Array<IntArray>): IntArray {
val g = Array(n) { mutableListOf<Int>() }
for (e in edges) {
g[e[0]-1].add(e[1]-1)
g[e[1]-1].add(e[0]-1)
}
val ans = IntArray(n-1)
for (mask in1 until (1 shl n)) {
if (Integer.bitCount(mask) < 2) continueval nodes = mutableListOf<Int>()
for (i in0 until n) if ((mask and (1 shl i)) !=0) nodes.add(i)
val vis = BooleanArray(n)
val q = ArrayDeque<Int>()
q.add(nodes[0]); vis[nodes[0]] = truevar cnt = 1while (q.isNotEmpty()) {
val u = q.removeFirst()
for (v in g[u]) {
if ((mask and (1 shl v)) !=0&& !vis[v]) {
vis[v] = true; q.add(v); cnt++ }
}
}
if (cnt != nodes.size) continuevar diam = 0for (u in nodes) {
val vis2 = BooleanArray(n)
val q2 = ArrayDeque<Pair<Int, Int>>()
q2.add(u to 0); vis2[u] = truewhile (q2.isNotEmpty()) {
val(v, d) = q2.removeFirst()
diam = maxOf(diam, d)
for (w in g[v]) {
if ((mask and (1 shl w)) !=0&& !vis2[w]) {
vis2[w] = true; q2.add(w to d+1)
}
}
}
}
if (diam > 0) ans[diam-1]++ }
return ans
}
}
classSolution:
defcountSubgraphsForEachDiameter(self, n: int, edges: list[list[int]]) -> list[int]:
from collections import deque
g = [[] for _ in range(n)]
for u, v in edges:
g[u-1].append(v-1)
g[v-1].append(u-1)
ans = [0] * (n-1)
for mask in range(1, 1<<n):
if bin(mask).count('1') <2:
continue nodes = [i for i in range(n) if (mask >> i) &1]
vis = [False] * n
q = deque([nodes[0]])
vis[nodes[0]] =True cnt =1while q:
u = q.popleft()
for v in g[u]:
if (mask >> v) &1andnot vis[v]:
vis[v] =True q.append(v)
cnt +=1if cnt != len(nodes):
continue diam =0for u in nodes:
vis2 = [False] * n
q2 = deque([(u, 0)])
vis2[u] =Truewhile q2:
v, d = q2.popleft()
diam = max(diam, d)
for w in g[v]:
if (mask >> w) &1andnot vis2[w]:
vis2[w] =True q2.append((w, d+1))
if diam >0:
ans[diam-1] +=1return ans