will this solution pass the provlem all testcases and no tle ensure think for as long as…
- User
will this solution pass the provlem all testcases and no tle ensure think for as long as possible go in depth """ from collections import defaultdict import sys
class Node: def init(self, name): self.name = name self.user_id = None # ID of the user who locked the node self.parent = None # Parent node self.children = [] # List of child nodes self.is_locked = False # Lock status of the node self.locked_descendant_count = 0 # Number of locked descendants self.in_time = 0 # Entry time during DFS traversal self.out_time = 0 # Exit time during DFS traversal
class Tree: def init(self, n, m, name_list, queries): self.node_list = [] self.node_map = {} # Map from node name to Node object
# Create nodes and map their names for name in name_list: node = Node(name) self.node_list.append(node) self.node_map[name] = node # Build the tree for i in range(n): parent = self.node_list[i] first_child_index = m * i + 1 for j in range(m): child_index = first_child_index + j if child_index < n: child = self.node_list[child_index] parent.children.append(child) child.parent = parent else: break self.root = self.node_list[0] self.user_locked_nodes = defaultdict(set) self.assign_time(self.root, [0]) # Preprocess for ancestry checks # Process queries self.processed_queries = [] for query in queries: self.processed_queries.append(query.strip().split()) def assign_time(self, node, time): # Assign entry time (in_time) node.in_time = time[0] time[0] += 1 for child in node.children: self.assign_time(child, time) # Assign exit time (out_time) node.out_time = time[0] time[0] += 1 def lock(self, node, user_id): # If the node is already locked, return False if node.is_locked: return False # If any ancestors are locked, cannot lock this node if self.is_ancestor_locked(node): return False # If any descendants are locked, cannot lock this node if node.locked_descendant_count > 0: return False # Lock the node node.is_locked = True node.user_id = user_id self.user_locked_nodes[user_id].add(node) # Update the locked_descendant_count for ancestors parent = node.parent while parent: parent.locked_descendant_count += 1 parent = parent.parent return True def unlock(self, node, user_id): # The node must be locked and the user_id must match if not node.is_locked or node.user_id != user_id: return False # Unlock the node node.is_locked = False node.user_id = None self.user_locked_nodes[user_id].remove(node) # Update the locked_descendant_count for ancestors parent = node.parent while parent: parent.locked_descendant_count -= 1 parent = parent.parent return True def upgrade(self, node, user_id): # The node must not be locked if node.is_locked: return False # If any ancestors are locked, cannot upgrade this node if self.is_ancestor_locked(node): return False # The node must have at least one locked descendant if node.locked_descendant_count == 0: return False # Collect locked descendants locked by user_id locked_descendants = [] success = self.collect_locked_descendants(node, user_id, locked_descendants) if not success: return False # Unlock all locked descendants for descendant in locked_descendants: descendant.is_locked = False descendant.user_id = None self.user_locked_nodes[user_id].remove(descendant) # Update the locked_descendant_count for ancestors parent = descendant.parent while parent: parent.locked_descendant_count -= 1 parent = parent.parent # Lock the current node node.is_locked = True node.user_id = user_id self.user_locked_nodes[user_id].add(node) # Update the locked_descendant_count for ancestors parent = node.parent while parent: parent.locked_descendant_count += 1 parent = parent.parent return True def collect_locked_descendants(self, node, user_id, locked_descendants): for child in node.children: if child.is_locked: if child.user_id == user_id: locked_descendants.append(child) else: return False # Found a locked node by another user elif child.locked_descendant_count > 0: if not self.collect_locked_descendants(child, user_id, locked_descendants): return False return True def is_ancestor_locked(self, node): current = node.parent while current: if current.is_locked: return True current = current.parent return False def process_queries(self): results = [] for query in self.processed_queries: operation_type, node_name, user_id = query node = self.node_map[node_name] user_id = int(user_id) if operation_type == "1": result = self.lock(node, user_id) elif operation_type == "2": result = self.unlock(node, user_id) elif operation_type == "3": result = self.upgrade(node, user_id) else: result = False # Invalid operation results.append('true' if result else 'false') return results - Assistant