will this code pass the problems staremnt all testcases and no tle analyze it perfectly…
- User
will this code pass the problems staremnt all testcases and no tle analyze it perfectly """ import sys sys.setrecursionlimit(1 << 25) # Increase recursion limit for large trees
class Node: def init(self, val): self.val = val self.uid = None self.parent = None self.children = [] self.isLocked = False self.lockedDescendantCount = 0 # Instead of a set, we use a count
class Tree: def init(self, m, n, name_list): self.node_map = {} self.node_list = [Node(name) for name in name_list] for node in self.node_list: self.node_map[node.val] = node
# Build the m-ary tree for i in range(n): for j in range(1, m + 1): child_index = m * i + j if child_index < n: parent = self.node_list[i] child = self.node_list[child_index] parent.children.append(child) child.parent = parent self.root = self.node_list[0] if n > 0 else None def lock_node(self, node, uid): if node.isLocked: return False # Check if any ancestor is locked current = node.parent while current: if current.isLocked: return False current = current.parent # Check if any descendant is locked if node.lockedDescendantCount > 0: return False # Lock the node node.isLocked = True node.uid = uid # Update lockedDescendantCount for all ancestors current = node.parent while current: current.lockedDescendantCount += 1 current = current.parent return True def unlock_node(self, node, uid): if not node.isLocked or node.uid != uid: return False # Unlock the node node.isLocked = False node.uid = None # Update lockedDescendantCount for all ancestors current = node.parent while current: current.lockedDescendantCount -= 1 current = current.parent return True def upgrade_lock(self, node, uid): if node.isLocked: return False # Check if any ancestor is locked current = node.parent while current: if current.isLocked: return False current = current.parent # Check if there is at least one locked descendant if node.lockedDescendantCount == 0: return False # Collect all locked descendants locked_nodes = [] self.collect_locked_descendants(node, locked_nodes) if not locked_nodes: return False # Ensure all locked descendants have the same uid for ln in locked_nodes: if ln.uid != uid: return False # Unlock all locked descendants for ln in locked_nodes: ln.isLocked = False ln.uid = None # Update lockedDescendantCount for ancestors ancestor = ln.parent while ancestor: ancestor.lockedDescendantCount -= 1 ancestor = ancestor.parent # Lock the current node node.isLocked = True node.uid = uid # Update lockedDescendantCount for all ancestors current = node.parent while current: current.lockedDescendantCount += 1 current = current.parent return True def collect_locked_descendants(self, node, locked_nodes): # DFS to collect all locked descendants stack = [node] while stack: current = stack.pop() for child in current.children: if child.isLocked: locked_nodes.append(child) if child.lockedDescendantCount > 0: stack.append(child) 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, None) if not node: results.append('false') continue if operation == "1": result = self.lock_node(node, uid) elif operation == "2": result = self.unlock_node(node, uid) elif operation == "3": result = self.upgrade_lock(node, uid) else: result = False # Invalid operation results.append('true' if result else 'false') return results - Assistant