/
brute_force.rs
120 lines (96 loc) · 3.8 KB
/
brute_force.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
pub struct Solution;
// ------------------------------------------------------ snip ------------------------------------------------------ //
use std::convert::TryInto;
enum ChildrenDepths {
None,
Single(u8),
Multiple(u8, u8),
}
impl Solution {
fn dfs(graph: &[u16; 16], visited: &mut u16, node: u8) -> (u8, u8) {
let mut children = graph[usize::from(node)];
let mut max_distance = 0;
let mut max_depth = 0;
let mut children_depths = ChildrenDepths::None;
while children != 0 {
let child = children.trailing_zeros() as _;
children &= children - 1;
let probe = 1_u16 << child;
if *visited & probe == 0 {
*visited |= probe;
let (child_max_distance, child_depth) = Self::dfs(graph, visited, child);
max_distance = max_distance.max(child_max_distance);
max_depth = max_depth.max(child_depth + 1);
children_depths = match children_depths {
ChildrenDepths::None => ChildrenDepths::Single(child_depth),
ChildrenDepths::Single(depth) => {
if child_depth > depth {
ChildrenDepths::Multiple(child_depth, depth)
} else {
ChildrenDepths::Multiple(depth, child_depth)
}
}
ChildrenDepths::Multiple(existing_depth_1, existing_depth_2) => {
if child_depth > existing_depth_2 {
if child_depth > existing_depth_1 {
ChildrenDepths::Multiple(child_depth, existing_depth_1)
} else {
ChildrenDepths::Multiple(existing_depth_1, child_depth)
}
} else {
continue;
}
}
}
}
}
(
max_distance.max(match children_depths {
ChildrenDepths::None => 0,
ChildrenDepths::Single(depth) => depth + 1,
ChildrenDepths::Multiple(depth_1, depth_2) => depth_1 + depth_2 + 2,
}),
max_depth,
)
}
pub fn count_subgraphs_for_each_diameter(n: i32, edges: Vec<Vec<i32>>) -> Vec<i32> {
let n = n as u32 as usize;
let mut graph = [0_u16; 16];
for edge in edges {
let [from, to]: [_; 2] = edge.try_into().ok().unwrap();
let from = from - 1;
let to = to - 1;
graph[from as u32 as usize] |= 1 << to;
graph[to as u32 as usize] |= 1 << from;
}
let mut result = vec![0; n - 1];
for subset in 3_u16..(1 << n) {
if !subset.is_power_of_two() {
let mut subgraph = graph;
for neighbor in &mut subgraph[..n] {
*neighbor &= subset;
}
let node = subset.trailing_zeros() as _;
let mut visited = 1_u16 << node;
let candidate_max_distance = Self::dfs(&subgraph, &mut visited, node).0;
if visited == subset {
result[usize::from(candidate_max_distance) - 1] += 1;
}
}
}
result
}
}
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl super::Solution for Solution {
fn count_subgraphs_for_each_diameter(n: i32, edges: Vec<Vec<i32>>) -> Vec<i32> {
Self::count_subgraphs_for_each_diameter(n, edges)
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}