Question: print ( Hello World ) import sys from collections import deque class Node: def _ _ init _ _ ( self , v ,
printHello World"
import sys
from collections import deque
class Node:
def initself v parentNone:
self.v v
self.children
self.parent parent
self.ancestorlocked
self.descendantlocked
self.userid
self.islocked False
def addchildrenself childlabels, parentnode:
for childlabel in childlabels:
self.children.appendNodechildlabel, parentnode
def buildtreerootmnodes:
q dequeroot
st
while q:
rqpopleft
if st lennodes:
continue
temp nodesst:stm
raddchildrentempr
st m
for link in rchildren:
qappendlink
return root
class Tree:
def initself treeroot:
self.root treeroot
self.vton
self.outputlog
self.fillvtonselfroot
def fillvtonself currentnode:
if not currentnode:
return
self.vtoncurrentnode.vcurrentnode, set
for child in currentnode.children:
self.fillvtonchild
def locknodeself v id:
t self.vton.getvNone
if tislocked or lenselfvtonv or tdescendantlocked :
return False
curr tparent
while curr:
if curr.islocked:
return False
curr curr.parent
while curr:
curr.descendantlocked
self.vtoncurrvaddt
curr curr.parent
tislocked True
tuserid id
return True
def unlocknodeself v id:
t self.vton.getvNone
if not t or not tislocked or tuserid id:
return False
curr tparent
while curr:
curr.descendantlocked
if t in self.vtoncurrv:
self.vtoncurrvremovet
curr curr.parent
tislocked False
return True
def upgradenodeself v id:
t self.vton.getvNone
if not t or tislocked or tancestorlocked or tdescendantlocked :
return False
lockeddescendants listselfvtonv
for lockeddescendant in lockeddescendants:
if lockeddescendant.userid id:
return False
for lockeddescendant in lockeddescendants:
self.unlocknodelockeddescendant.v id
self.locknodev id
return True
if namemain:
import sys
input sysstdin.read
data inputsplit
n intdata
m intdata
q intdata
nodes data: n
queries data n:
root Nodenodes None
root buildtreeroot m nodes
tree Treeroot
idx
results
while idx lenqueries:
op intqueriesidx
sv queriesidx
uid intqueriesidx
idx
if op :
results.appendtrue if tree.locknodesv uid else "false"
elif op :
results.appendtrue if tree.unlocknodesv uid else "false"
elif op :
results.appendtrue if tree.upgradenodesv uid else "false"
for result in results:
printresult
Make above code
Lets say you are running the lockunlock in a multi core machine. Now you
want to let multiple threads to run lock As we saw in part A locking a
node has multiple validations inside. Will doing lock on two nodes
cause a race condition. If yes, how will you solve it In short, how do
make the lock function thread safe? Multiple threads running it
simultaneously shouldnt not affect the correctness. Try to make the
critical sections more granular. ie dont create any big
atomicsynchronised blocks that will make parallelism suffer. Consider
each operation is atomic.
if run in a multithreadedmachine
without using threading library by using valiables to make function mutually exclusive
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
