why is this not working """ import sys import time class Node: def init(self, val):…
- User
why is this not working """ import sys import time
class Node: def init(self, val): self.val = val self.uid = None self.parent = None self.child = [] self.isLocked = False self.lockedDescendantCount = 0 self.modifying = False # Custom flag to indicate modification in progress
class Tree: def init(self, m, n, name_list): self.node_list = [] self.node_map = {} self.locked_nodes = set() self.time = 0 self.in_time = {} self.out_time = {}
for name in name_list: node = Node(name) self.node_list.append(node) self.node_map[name] = node for i in range(n): for j in range(m): child_index = m * i + j + 1 if child_index < n: self.node_list[i].child.append(self.node_list[child_index]) self.node_list[child_index].parent = self.node_list[i] self.root = self.node_list[0] self.dfs(self.root) def dfs(self, node): self.time += 1 self.in_time[node] = self.time for child in node.child: self.dfs(child) self.time += 1 self.out_time[node] = self.time def is_descendant(self, ancestor, node): """Check if `node` is a descendant of `ancestor` using in-time and out-time.""" return self.in_time[ancestor] < self.in_time[node] and self.out_time[node] < self.out_time[ancestor] def acquire_modification_lock(self, node): """Custom spin-lock to acquire modification access to a node.""" while True: # Attempt to acquire the lock by setting 'modifying' to True if not node.modifying: node.modifying = True break # Busy-wait (spin) until the node becomes available time.sleep(0.0001) # Sleep briefly to reduce CPU usage def release_modification_lock(self, node): """Release the modification lock on a node.""" node.modifying = False def lock(self, node, uid): # Step 1: Collect all ancestors including the node itself ancestors = [] current = node while current: ancestors.append(current) current = current.parent ancestors.reverse() # Lock from root to the target node # Step 2: Acquire all necessary modification locks in order for n in ancestors: self.acquire_modification_lock(n) try: # Perform validations if node.isLocked: return False if self.isAncestorLocked(node): return False if node.lockedDescendantCount > 0: return False # Lock the node node.isLocked = True node.uid = uid self.locked_nodes.add(node) # Update lockedDescendantCount for ancestors parent = node.parent while parent: self.acquire_modification_lock(parent) parent.lockedDescendantCount += 1 self.release_modification_lock(parent) parent = parent.parent return True finally: # Release all modification locks in reverse order for n in reversed(ancestors): self.release_modification_lock(n) def unlock(self, node, uid): # Step 1: Acquire modification lock on the node self.acquire_modification_lock(node) try: if not node.isLocked or node.uid != uid: return False # Unlock the node node.isLocked = False node.uid = None self.locked_nodes.remove(node) # Update lockedDescendantCount for ancestors parent = node.parent while parent: self.acquire_modification_lock(parent) parent.lockedDescendantCount -= 1 self.release_modification_lock(parent) parent = parent.parent return True finally: # Release modification lock on the node self.release_modification_lock(node) def upgrade(self, node, uid): # Step 1: Acquire modification locks on the node and all its ancestors ancestors = [] current = node while current: ancestors.append(current) current = current.parent ancestors.reverse() # Lock from root to the target node # Acquire all modification locks for n in ancestors: self.acquire_modification_lock(n) try: # Perform validations for upgrade if node.isLocked: return False if self.isAncestorLocked(node): return False if node.lockedDescendantCount == 0: return False # Collect all locked descendants locked_descendants = [] stack = [node] while stack: current = stack.pop() for child in current.child: if child.isLocked: locked_descendants.append(child) stack.append(child) # Ensure all locked descendants are locked by the same uid for descendant in locked_descendants: if descendant.uid != uid: return False # Unlock all locked descendants for descendant in locked_descendants: self.acquire_modification_lock(descendant) descendant.isLocked = False descendant.uid = None self.locked_nodes.remove(descendant) # Update lockedDescendantCount for ancestors parent = descendant.parent while parent: self.acquire_modification_lock(parent) parent.lockedDescendantCount -= 1 self.release_modification_lock(parent) parent = parent.parent self.release_modification_lock(descendant) # Lock the current node node.isLocked = True node.uid = uid self.locked_nodes.add(node) # Update lockedDescendantCount for ancestors parent = node.parent while parent: self.acquire_modification_lock(parent) parent.lockedDescendantCount += 1 self.release_modification_lock(parent) parent = parent.parent return True finally: # Release all modification locks in reverse order for n in reversed(ancestors): self.release_modification_lock(n) def isAncestorLocked(self, node): """Check if any of the ancestors of the given node are locked.""" current = node.parent while current: # Acquire modification lock to read `isLocked` self.acquire_modification_lock(current) try: if current.isLocked: return True current = current.parent finally: self.release_modification_lock(current) return False def process_queries(self, queries): results = [] for query in queries: parts = query.strip().split() if len(parts) != 3: results.append('false') continue operation, node_name, uid = parts uid = int(uid) node = self.node_map.get(node_name) if not node: results.append('false') continue if operation == "1": result = self.lock(node, uid) elif operation == "2": result = self.unlock(node, uid) elif operation == "3": result = self.upgrade(node, uid) else: result = False # Invalid operation results.append('true' if result else 'false') return results - Assistant