-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoperations-on-tree.rs
107 lines (87 loc) · 2.71 KB
/
operations-on-tree.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
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
struct LockingTree {
children: std::collections::HashMap<i32, Vec<i32>>,
parents: std::collections::HashMap<i32, i32>,
/// node: user_id
locked: std::collections::HashMap<i32, i32>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
/**
* Your LockingTree object will be instantiated and called as such:
* let obj = LockingTree::new(parent);
* let ret_1: bool = obj.lock(num, user);
* let ret_2: bool = obj.unlock(num, user);
* let ret_3: bool = obj.upgrade(num, user);
*/
impl LockingTree {
fn new(parent: Vec<i32>) -> Self {
let mut children = std::collections::HashMap::<i32, Vec<i32>>::new();
let mut parents = std::collections::HashMap::new();
for i in 0..parent.len() {
children
.entry(parent[i])
.and_modify(|x| x.push(i as _))
.or_insert(vec![i as _]);
parents.insert(i as _, parent[i]);
}
Self {
children,
parents,
locked: std::collections::HashMap::new(),
}
}
fn lock(&mut self, num: i32, user: i32) -> bool {
if self.locked.contains_key(&num) {
return false;
}
self.locked.insert(num, user);
true
}
fn unlock(&mut self, num: i32, user: i32) -> bool {
match self.locked.get(&num) {
Some(&x) if x == user => {}
_ => return false,
}
self.locked.remove(&num);
true
}
fn upgrade(&mut self, num: i32, user: i32) -> bool {
if self.locked.contains_key(&num) {
return false;
}
if self.check_parent(num) {
return false;
}
if !self.unlock_sons(num) {
return false;
}
self.locked.insert(num, user);
true
}
/// 解锁子孙。如果有一个子孙已加锁,返回true。
fn unlock_sons(&mut self, num: i32) -> bool {
let len = if let Some(x) = self.children.get(&num) {
x.len()
} else {
return false;
};
let mut result = false;
for i in 0..len {
result |= self.locked.remove(&self.children[&num][i]).is_some();
result |= self.unlock_sons(self.children[&num][i]);
}
result
}
/// 检查父节点,如果都是未加锁,返回true
fn check_parent(&self, num: i32) -> bool {
match self.parents.get(&num) {
Some(&x) => self.locked.contains_key(&x) || self.check_parent(x),
None => false,
}
}
}