Skip to content

Commit 97ce307

Browse files
committed
1 parent fb5e238 commit 97ce307

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1219,3 +1219,4 @@ mod s1611_minimum_one_bit_operations_to_make_integers_zero;
12191219
mod s1614_maximum_nesting_depth_of_the_parentheses;
12201220
mod s1615_maximal_network_rank;
12211221
mod s1616_split_two_strings_to_make_palindrome;
1222+
mod s1617_count_subtrees_with_max_distance_between_cities;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* [1617] Count Subtrees With Max Distance Between Cities
3+
*
4+
* 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.
5+
*
6+
* 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.
7+
*
8+
* 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.
9+
*
10+
* Return an array of size n-1 where the d^th element (1-indexed) is the number of subtrees in which the maximum distance between any two cities is equal to d.
11+
*
12+
* Notice that the distance between the two cities is the number of edges in the path between them.
13+
*
14+
*
15+
* Example 1:
16+
*
17+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/p1.png" style="width: 161px; height: 181px;" />
18+
*
19+
*
20+
* Input: n = 4, edges = [[1,2],[2,3],[2,4]]
21+
* Output: [3,4,0]
22+
* Explanation:
23+
* The subtrees with subsets {1,2}, {2,3} and {2,4} have a max distance of 1.
24+
* The subtrees with subsets {1,2,3}, {1,2,4}, {2,3,4} and {1,2,3,4} have a max distance of 2.
25+
* No subtree has two nodes where the max distance between them is 3.
26+
*
27+
*
28+
* Example 2:
29+
*
30+
*
31+
* Input: n = 2, edges = [[1,2]]
32+
* Output: [1]
33+
*
34+
*
35+
* Example 3:
36+
*
37+
*
38+
* Input: n = 3, edges = [[1,2],[2,3]]
39+
* Output: [2,1]
40+
*
41+
*
42+
*
43+
* Constraints:
44+
*
45+
*
46+
* 2 <= n <= 15
47+
* edges.length == n-1
48+
* edges[i].length == 2
49+
* 1 <= ui, vi <= n
50+
* All pairs (ui, vi) are distinct.
51+
*
52+
*/
53+
pub struct Solution {}
54+
55+
// problem: https://leetcode.com/problems/count-subtrees-with-max-distance-between-cities/
56+
// discuss: https://leetcode.com/problems/count-subtrees-with-max-distance-between-cities/discuss/?currentPage=1&orderBy=most_votes&query=
57+
58+
// submission codes start here
59+
60+
impl Solution {
61+
// Credit: https://leetcode.com/problems/count-subtrees-with-max-distance-between-cities/solutions/3181795/just-a-runnable-solution/
62+
pub fn count_subgraphs_for_each_diameter(n: i32, edges: Vec<Vec<i32>>) -> Vec<i32> {
63+
let mut g = vec![vec![999; n as usize]; n as usize];
64+
65+
for e in edges.iter() {
66+
let i = (e[0] - 1) as usize;
67+
let j = (e[1] - 1) as usize;
68+
g[i][j] = 1;
69+
g[j][i] = 1;
70+
}
71+
72+
for k in 0..n as usize {
73+
for i in 0..n as usize {
74+
for j in 0..n as usize {
75+
g[i][j] = g[i][j].min(g[i][k] + g[k][j]);
76+
}
77+
}
78+
}
79+
80+
let mut result = vec![0; n as usize - 1];
81+
82+
for s in 0_i32..(1 << n) {
83+
let k = s.count_ones() as i32;
84+
let (mut e, mut d) = (0_i32, 0_i32);
85+
for (i, item) in g.iter().enumerate().take(n as usize) {
86+
if s & (1 << i) != 0 {
87+
for (j, &item_j) in item.iter().enumerate().take(n as usize).skip(i + 1) {
88+
if s & (1 << j) != 0 {
89+
e += i32::from(g[i][j] == 1);
90+
d = d.max(item_j);
91+
}
92+
}
93+
}
94+
}
95+
96+
if e == k - 1 && d > 0 {
97+
result[(d - 1) as usize] += 1;
98+
}
99+
}
100+
101+
result
102+
}
103+
}
104+
105+
// submission codes end
106+
107+
#[cfg(test)]
108+
mod tests {
109+
use super::*;
110+
111+
#[test]
112+
fn test_1617_example_1() {}
113+
}

0 commit comments

Comments
 (0)