|
| 1 | +/** |
| 2 | + * 1617. Count Subtrees With Max Distance Between Cities |
| 3 | + * https://leetcode.com/problems/count-subtrees-with-max-distance-between-cities/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * There are n cities numbered from 1 to n. You are given an array edges of size n-1, where |
| 7 | + * edges[i] = [ui, vi] represents a bidirectional edge between cities ui and vi. There exists |
| 8 | + * a unique path between each pair of cities. In other words, the cities form a tree. |
| 9 | + * |
| 10 | + * A subtree is a subset of cities where every city is reachable from every other city in the |
| 11 | + * subset, where the path between each pair passes through only the cities from the subset. |
| 12 | + * Two subtrees are different if there is a city in one subtree that is not present in the other. |
| 13 | + * |
| 14 | + * For each d from 1 to n-1, find the number of subtrees in which the maximum distance between |
| 15 | + * any two cities in the subtree is equal to d. |
| 16 | + * |
| 17 | + * Return an array of size n-1 where the dth element (1-indexed) is the number of subtrees in |
| 18 | + * which the maximum distance between any two cities is equal to d. |
| 19 | + * |
| 20 | + * Notice that the distance between the two cities is the number of edges in the path between them. |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * @param {number} n |
| 25 | + * @param {number[][]} edges |
| 26 | + * @return {number[]} |
| 27 | + */ |
| 28 | +var countSubgraphsForEachDiameter = function(n, connections) { |
| 29 | + const adjacencyList = Array.from({ length: n }, () => []); |
| 30 | + for (const [u, v] of connections) { |
| 31 | + adjacencyList[u - 1].push(v - 1); |
| 32 | + adjacencyList[v - 1].push(u - 1); |
| 33 | + } |
| 34 | + |
| 35 | + const diameterCounts = new Array(n - 1).fill(0); |
| 36 | + |
| 37 | + for (let mask = 1; mask < 1 << n; mask++) { |
| 38 | + const selectedNodes = Array(n).fill(0); |
| 39 | + let nodeCount = 0; |
| 40 | + for (let i = 0; i < n; i++) { |
| 41 | + if (mask & (1 << i)) { |
| 42 | + selectedNodes[i] = 1; |
| 43 | + nodeCount++; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (nodeCount < 2) continue; |
| 48 | + |
| 49 | + const start = selectedNodes.findIndex(bit => bit); |
| 50 | + const { maxDist: dist1, farthestNode, distances } = findMaxDistance(selectedNodes, start); |
| 51 | + |
| 52 | + if (distances.some((d, i) => selectedNodes[i] && d === -1)) continue; |
| 53 | + |
| 54 | + const { maxDist: dist2 } = findMaxDistance(selectedNodes, farthestNode); |
| 55 | + |
| 56 | + if (dist2 > 0) { |
| 57 | + diameterCounts[dist2 - 1]++; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return diameterCounts; |
| 62 | + |
| 63 | + function findMaxDistance(nodes, start) { |
| 64 | + const distances = new Array(n).fill(-1); |
| 65 | + const queue = [start]; |
| 66 | + distances[start] = 0; |
| 67 | + let maxDist = 0; |
| 68 | + let farthestNode = start; |
| 69 | + |
| 70 | + while (queue.length) { |
| 71 | + const current = queue.shift(); |
| 72 | + for (const neighbor of adjacencyList[current]) { |
| 73 | + if (nodes[neighbor] && distances[neighbor] === -1) { |
| 74 | + distances[neighbor] = distances[current] + 1; |
| 75 | + if (distances[neighbor] > maxDist) { |
| 76 | + maxDist = distances[neighbor]; |
| 77 | + farthestNode = neighbor; |
| 78 | + } |
| 79 | + queue.push(neighbor); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return { maxDist, farthestNode, distances }; |
| 85 | + } |
| 86 | +}; |
0 commit comments