# 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:

• 2 <= n <= 15
• edges.length == n-1
• edges[i].length == 2
• 1 <= ui, vi <= n
• All pairs (ui, vi) are distinct.

## Solutions

• class Solution {
private List<Integer>[] g;
private int msk;
private int nxt;
private int mx;

public int[] countSubgraphsForEachDiameter(int n, int[][] edges) {
g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (int[] e : edges) {
int u = e[0] - 1, v = e[1] - 1;
g[u].add(v);
g[v].add(u);
}
int[] ans = new int[n - 1];
for (int mask = 1; mask < 1 << n; ++mask) {
if ((mask & (mask - 1)) == 0) {
continue;
}
msk = mask;
mx = 0;
int cur = 31 - Integer.numberOfLeadingZeros(msk);
dfs(cur, 0);
if (msk == 0) {
msk = mask;
mx = 0;
dfs(nxt, 0);
++ans[mx - 1];
}
}
return ans;
}

private void dfs(int u, int d) {
msk ^= 1 << u;
if (mx < d) {
mx = d;
nxt = u;
}
for (int v : g[u]) {
if ((msk >> v & 1) == 1) {
dfs(v, d + 1);
}
}
}
}

• class Solution {
public:
vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {
vector<vector<int>> g(n);
for (auto& e : edges) {
int u = e[0] - 1, v = e[1] - 1;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
vector<int> ans(n - 1);
int nxt = 0, msk = 0, mx = 0;
function<void(int, int)> dfs = [&](int u, int d) {
msk ^= 1 << u;
if (mx < d) {
mx = d;
nxt = u;
}
for (int& v : g[u]) {
if (msk >> v & 1) {
dfs(v, d + 1);
}
}
};
for (int mask = 1; mask < 1 << n; ++mask) {
if ((mask & (mask - 1)) == 0) {
continue;
}
msk = mask;
mx = 0;
int cur = 31 - __builtin_clz(msk);
dfs(cur, 0);
if (msk == 0) {
msk = mask;
mx = 0;
dfs(nxt, 0);
++ans[mx - 1];
}
}
return ans;
}
};

• 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


• func countSubgraphsForEachDiameter(n int, edges [][]int) []int {
g := make([][]int, n)
for _, e := range edges {
u, v := e[0]-1, e[1]-1
g[u] = append(g[u], v)
g[v] = append(g[v], u)
}
ans := make([]int, n-1)
var msk, nxt, mx int
var dfs func(int, int)
dfs = func(u, d int) {
msk ^= 1 << u
if mx < d {
mx, nxt = d, u
}
for _, v := range g[u] {
if msk>>v&1 == 1 {
dfs(v, d+1)
}
}
}
for mask := 1; mask < 1<<n; mask++ {
if mask&(mask-1) == 0 {
continue
}
msk, mx = mask, 0
cur := bits.Len(uint(msk)) - 1
dfs(cur, 0)
if msk == 0 {
msk, mx = mask, 0
dfs(nxt, 0)
ans[mx-1]++
}
}
return ans
}

• function countSubgraphsForEachDiameter(n: number, edges: number[][]): number[] {
const g = Array.from({ length: n }, () => []);
for (const [u, v] of edges) {
g[u - 1].push(v - 1);
g[v - 1].push(u - 1);
}
const ans: number[] = new Array(n - 1).fill(0);
let [mx, msk, nxt] = [0, 0, 0];
const dfs = (u: number, d: number) => {
if (mx < d) {
mx = d;
nxt = u;
}
msk ^= 1 << u;
for (const v of g[u]) {
if ((msk >> v) & 1) {
dfs(v, d + 1);
}
}
};
for (let mask = 1; mask < 1 << n; ++mask) {
if ((mask & (mask - 1)) === 0) {
continue;
}
msk = mask;
mx = 0;
const cur = 31 - numberOfLeadingZeros(msk);
dfs(cur, 0);
if (msk === 0) {
msk = mask;
mx = 0;
dfs(nxt, 0);
++ans[mx - 1];
}
}
return ans;
}

function numberOfLeadingZeros(i: number): number {
if (i == 0) return 32;
let n = 1;
if (i >>> 16 == 0) {
n += 16;
i <<= 16;
}
if (i >>> 24 == 0) {
n += 8;
i <<= 8;
}
if (i >>> 28 == 0) {
n += 4;
i <<= 4;
}
if (i >>> 30 == 0) {
n += 2;
i <<= 2;
}
n -= i >>> 31;
return n;
}