[LeetCode] Operations on Tree

1993. Operations on Tree

You are given a tree with n nodes numbered from 0 to n - 1 in the form of a parent array parent where parent[i] is the parent of the ith node. The root of the tree is node 0, so parent[0] = -1 since it has no parent. You want to design a data structure that allows users to lock, unlock, and upgrade nodes in the tree.

The data structure should support the following functions:

  • Lock: Locks the given node for the given user and prevents other users from locking the same node. You may only lock a node using this function if the node is unlocked.
  • Unlock: Unlocks the given node for the given user. You may only unlock a node using this function if it is currently locked by the same user.
  • Upgrade: Locks the given node for the given user and unlocks all of its descendants regardless of who locked it. You may only upgrade a node if all 3 conditions are true:
  • The node is unlocked,
  • It has at least one locked descendant (by any user), and
  • It does not have any locked ancestors.

Implement the LockingTree class:

  • LockingTree(int[] parent) initializes the data structure with the parent array.
  • lock(int num, int user) returns true if it is possible for the user with id user to lock the node num, or false otherwise. If it is possible, the node num will become locked by the user with id user.
  • unlock(int num, int user) returns true if it is possible for the user with id user to unlock the node num, or false otherwise. If it is possible, the node num will become unlocked.
  • upgrade(int num, int user) returns true if it is possible for the user with id user to upgrade the node num, or false otherwise. If it is possible, the node num will be upgraded.
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
class LockingTree {
vector<vector<int>> adj, radj;
unordered_map<int, int> l;
bool updfs(int u) {
while(!radj[u].empty()) {
if(l.count(u)) return false;
u = radj[u][0];
}
return !l.count(u);
}
bool downdfs(int u) {
bool res = l.count(u);
if(l.count(u)) l.erase(u);
for(auto v : adj[u]) {
res |= downdfs(v);
}
return res;
}
public:
LockingTree(vector<int>& A) {
adj = radj = vector<vector<int>>(A.size());
for(int i = 0; i < A.size(); i++) {
if(A[i] == -1) continue;
adj[A[i]].push_back(i);
radj[i].push_back(A[i]);
}
}

bool lock(int num, int user) {
if(l.count(num)) return false;
l[num] = user;
return true;
}

bool unlock(int num, int user) {
if(!l.count(num) or l[num] != user) return false;
l.erase(num);
return true;
}

bool upgrade(int num, int user) {
if(l.count(num)) return false;
if(!updfs(num)) return false;
if(!downdfs(num)) return false;
return lock(num, user);
}
};

/**
* Your LockingTree object will be instantiated and called as such:
* LockingTree* obj = new LockingTree(parent);
* bool param_1 = obj->lock(num,user);
* bool param_2 = obj->unlock(num,user);
* bool param_3 = obj->upgrade(num,user);
*/
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/25/PS/LeetCode/operations-on-tree/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.