-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathLocking The Tree of Space.cpp
368 lines (276 loc) · 9.77 KB
/
Locking The Tree of Space.cpp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
Solution by Rahul Surana
************************************************************************
Locking the tree of space
You have a world map represented as an M-Ary tree (sample tree below)
You need to define three operations on it
lock(X, uid)
unlock(X, uid)
upgradeLock(x, uid)
where X the name of a node in the tree (that would be unique) and uid is the user who is performing the operation
Here are the definitions for the Operations:
Lock(X, uid)
Lock takes an exclusive access on the subtree rooted at X. It is formally defined like this:<br>
Once lock(X, uid) succeeds, then:
lock(A, anyUserId) should fail (returns false), where A is a descendent of X,
lock(B, anyUserId) should fail (returns false), where X is a descendent of B
Lock operation cannot be performed on a node which is already locked i.e. lock(X, anyUserId) should fail (returns false).
Unlock(X, uid)
Unlock reverts what was done by the Lock operation. It can only be called on same node on which user uid had called a Lock on before. Returns true if it is successful.
UpgradeLock(X, uid)
It helps the user uid upgrade their lock to an ancestor node. It is only possible if the node X already has locked descendants and all of them are only locked by the same user uid. Upgrade should fail if there is any node which is descendant of X that is locked by a different user. Successful Upgrade will 'Lock' the node. UpgradeLock call shouldn't violate the consistency model that Lock/Unlock function requires.
Notes
1) The number of nodes in the tree N is very large. So, optimize the time complexity for the above algorithms
2) The below section contains the input format.
The first line contains the number of Nodes in the tree (N).
The second line contains number of children per node (value m in m-ary Tree).
The third line contains number of queries (Q).
Next N lines contains the NodeName (string) in the m-Ary Tree.
Next Q lines contains queries which are in format: OperationType NodeName UserId
OperationType →<br>
1 for Lock<br>
2 for unlock<br>
3 for upgradeLock
NodeName → Name of any node (unique) in m-Ary Tree.
UserId → Integer value representing a unique user.
Example input:
7
2
3
World
Asia
Africa
China
India
SouthAfrica
Egypt
1 China 9
2 India 9
3 Asia 9
With the above input you represents a 2-ary tree with 7 nodes as follows:
World
/ \
Asia Africa
/ \ / \
China India SouthAfrica Egypt
Additional Notes:
1) Here ‘1 China 3’ indicates the following ‘OperationType NodeName UserId’
2) The tree is always fully balanced
3) Constraints on the inputs are as follows<br>
1 < N < 5 * 10^5<br>
1 < m < 30<br>
1 < Q < 5 * 10 ^ 5<br>
1 < length of NodeName < 20
4) Optimize the time complexity
Lock - O(log<sub>m</sub>N)
Unlock - O(log<sub>m</sub>N)
UpgradeLock - O(numberOfLockedNodes * log<sub>m</sub>N)
5) Lock operation on already locked node should fail.
6) Once UpgradeLock(X,uid) succeeds on X. It is equivalent to X being locked by uid. So, Lock(A/B, anyuser) should fail as per the definition of Lock and Unlock(X, uid) should also work. <br>
7) Upgrade lock operation on a node having no locked descendants should fail and upgrade lock on already locked node should also fail.
**************************************************************************
*/
#include <bits/stdc++.h>
#define ll long long
#define vl vector<ll>
#define vi vector<int>
#define pi pair<int,int>
#define pl pair<ll,ll>
#define all(a) a.begin(),a.end()
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define FOR(i,a) for(int i = 0; i < a; i++)
#define trace(x) cerr<<#x<<" : "<<x<<endl;
#define trace2(x,y) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<endl;
#define trace3(x,y,z) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<" | "<<#z<<" : "<<z<<endl;
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;
int inf=1e9+7, MOD=1e9+7;
int w = 1;
struct Tree{
public :
bool isLocked;
bool isLockable;
int id;
int ma;
Tree* p;
vector<Tree*> c;
Tree(){
isLocked = false;
isLockable = true;
id = -1;
ma = w++;
p = NULL;
}
Tree(Tree* parent){
isLocked = false;
isLockable = true;
id = -1;
ma = w++;
p = parent;
}
};
// bool ll(Tree* n) return n->l;
bool locking(Tree* n, int uuid){
if(n->isLockable == false || (n->isLocked)) return false;
Tree* T = n;
queue<Tree*> q;
q.push(T);
while(!q.empty()){
Tree* temp = q.front();
q.pop();
for(auto x: temp->c){
if(x->isLocked) return false;
q.push(x);
}
}
n->id = uuid;
n->isLocked = true;
q.push(T);
while(!q.empty()){
Tree* temp = q.front();
q.pop();
for(auto x: temp->c){
x->isLockable = false;
q.push(x);
}
}
return true;
}
bool unlocking(Tree* n,int uuid){
if(n->isLockable == false || n -> isLocked == false || (n->isLocked && uuid != n->id)) return false;
Tree* T = n;
n->isLocked= false;
n->id = -1;
queue<Tree*> q;
q.push(T);
while(!q.empty()){
Tree* temp = q.front();
q.pop();
for(auto x: temp->c){
x->isLockable = true;
q.push(x);
}
}
return true;
}
bool update(Tree* n,int uuid){
if(n->isLockable == false || n->isLocked == true) return false;
Tree* T = n;
queue<Tree*> q;
q.push(T);
bool f = false;
while(!q.empty()){
Tree* temp = q.front();
q.pop();
for(auto x: temp->c){
if(x->isLocked == true && x->id != uuid) { return false; }
if(x->isLocked) f = true;
q.push(x);
}
}
if(!f) return false;
// n->isLocked = true;
// n->id = uuid;
// queue<Tree*> q;
q.push(T);
while(!q.empty()){
Tree* temp = q.front();
q.pop();
for(auto x: temp->c){
if(x->isLocked){
// x->isLocked = false;
if(!unlocking(x,uuid)) return false;
}
q.push(x);
}
}
// locking(n,uuid);
return locking(n,uuid);
}
void Print(Tree* n){
queue<Tree*> q;
q.push(n);
// cout << n->ma <<"\n";
while(!q.empty()){
Tree* temp = q.front();
q.pop();
for(auto x: temp->c){
cout << temp->ma << " -> " << x->ma << " ";
}
}
}
unordered_map<string, Tree*> sToT;
int main(){
int n,m,q;
cin >> n >> m >>q;
// adj.resize(n+1,vector<int>());
// nuid.resize(n+1,-1);
// p.resize(n+1,-1);
// intime.resize(n+1,-1);
// outtime.resize(n+1,-1);
// p[1]=1;
// int x = n/m;
// for(int k = 1; k <= x; k++){
// for(int l = 1; l <= m; l++){
// adj[k].pb(m*(k-1)+l+1);
// p[m*(k-1)+l+1] = k;
// }
// }
// FOR(i,adj.size()) {
// FOR(j,p.size()) cout << j << " " <<p[j] <<" ";
// cout <<"\n";
// }
Tree* t = new Tree();
string d;
cin >> d;
// a.c.push_back(&t);
sToT[d] = t;
queue<Tree*> queue;
queue.push(t);
int k = 1;
while(!queue.empty()){
auto temp = queue.front();
queue.pop();
// cout << temp->ma <<"\n";
while(k < n && (int)temp->c.size() < m){
string s;
cin >> s;
// cout << s <<"\n";
Tree* u = new Tree(temp);
sToT[s] = u;
temp->c.push_back(u);
queue.push(u);
k++;
// cout << s <<" "<<sToT[s]->ma <<"\n";
}
}
// Print(t);
// dfs(1);
FOR(i,q) {
int qn,uuid;
string name;
bool ans;
cin >> qn >> name >> uuid;
if(qn == 1){
ans = locking(sToT[name],uuid);
// cout << name<<" "<< sToT[name]->id << " " << uuid << "\n";
}
else if(qn == 2){
ans = unlocking(sToT[name],uuid);
// cout << name<<" "<< sToT[name]->id << " " << uuid << "\n";
}
else {
ans = update(sToT[name],uuid);
// cout << ans << "\n";
}
// FOR(j,nuid.size()) cout << j << " " << nuid[j] <<" ";
// cout <<"\n";
if(ans) cout << "true\n";
else cout << "false\n";
}
return 0;
}