-
Notifications
You must be signed in to change notification settings - Fork 0
/
N_ArrTree.java
181 lines (132 loc) · 2.95 KB
/
N_ArrTree.java
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import java.util.*;
class N_ArrTree{
static class Node{
boolean isLocked;
int id;
Node parent;
int anc_locked;
int des_locked;
ArrayList<Node> child = new ArrayList<>();
}
static void change(Node node , int val){
if(node == null){
return;
}
node.anc_locked += val;
for(Node i : node.child){
change(i , val);
}
}
static boolean lock(Node node , int id){
if(node.isLocked){
return false;
}
if(node.anc_locked > 0 || node.des_locked > 0){
return false;
}
Node parent = node;
while(parent != null){
parent.des_locked += 1;
parent = parent.parent;
}
change(node , 1);
node.isLocked = true;
node.id = id;
return true;
}
static boolean unlock(Node node , int id){
if(!node.isLocked || node.id != id){
return false;
}
Node parent = node;
while(parent != null){
parent.des_locked -= 1;
parent = parent.parent;
}
change(node , -1);
node.isLocked = false;
node.id = 0;
return true;
}
static boolean getAllChilds(Node node , ArrayList<Node> a , int id){
if(node == null){
return true;
}
if(node.isLocked){
if(id != node.id)
return false;
else
a.add(node);
}
if(node.des_locked == 0){
return true;
}
// a.add(node);
for(Node i : node.child){
boolean ans = getAllChilds(i , a , id);
if(ans == false) return false;
}
return true;
}
static boolean upgrade(Node node , int id){
if(node.isLocked || node.anc_locked > 0 || node.des_locked == 0){
return false;
}
ArrayList<Node> a = new ArrayList<>();
boolean can = getAllChilds(node , a , id);
if(!can) return false;
change(node , 1);
for(Node i : a){
unlock(i , id);
}
node.isLocked = true;
node.id = id;
return true;
}
public static void main(String[] agrs){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int q = sc.nextInt();
HashMap<String , Node> hash = new HashMap<>();
String[] arr = new String[n];
for(int i=0 ; i<n; i++){
arr[i] = sc.next();
}
Node root = new Node();
// root.str = arr[0];
hash.put(arr[0] , root);
Queue<Node> que = new java.util.LinkedList<>();
que.add(root);
int index = 1;
while(que.size()>0 && index < n){
int size = que.size();
while(size-- > 0){
Node rem = que.remove();
for(int i=1; i<=k && index<n ; i++){
Node newNode = new Node();
newNode.parent = rem;
hash.put(arr[index] , newNode);
rem.child.add(newNode);
que.add(newNode);
index+=1;
}
}
}
for(int i=0 ; i<q; i++){
int val = sc.nextInt();
String str = sc.next();
int id = sc.nextInt();
boolean ans = false;
if(val == 1){
ans = lock(hash.get(str) , id);
}else if(val == 2){
ans = unlock(hash.get(str) , id);
}else if(val == 3){
ans = upgrade(hash.get(str) , id);
}
System.out.println(ans);
// System.out.println();
}
}
}