i want yout o extract the main part and like how the input is hanfles in this code and implement that into the second code 
"""
import sys

class Node:
    def __init__(self, val):
        self.val = val
        self.uid = None
        self.parent = None  
        self.child = [] 
        self.isLocked = False  
        self.locked_descendants = set()  # Set of locked descendant nodes

class Tree:
    def __init__(self, m, n, name_list):
        self.node_list = []
        self.node_map = {}
        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):
            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]

    def 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 any descendant is locked
        if node.locked_descendants:
            return False

        # Lock the node
        node.isLocked = True
        node.uid = uid

        # Update locked_descendants for all ancestors
        current = node.parent
        while current:
            current.locked_descendants.add(node)
            current = current.parent

        return True

    def unlock(self, node, uid):
        if not node.isLocked or node.uid != uid:
            return False

        # Unlock the node
        node.isLocked = False
        node.uid = None

        # Update locked_descendants for all ancestors
        current = node.parent
        while current:
            current.locked_descendants.discard(node)  # Correct
            current = current.parent

        return True

    def upgrade(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 not node.locked_descendants:
            return False

        # Collect all locked descendants
        locked_descendants = list(node.locked_descendants)

        # Check if all locked descendants have the same uid
        for ld in locked_descendants:
            if ld.uid != uid:
                return False

        # Unlock all locked descendants
        for ld in locked_descendants:
            ld.isLocked = False
            ld.uid = None
            node.locked_descendants.discard(ld)

            # Update locked_descendants for all ancestors of ld
            ancestor = ld.parent
            while ancestor:
                ancestor.locked_descendants.discard(ld)
                ancestor = ancestor.parent

        # Lock the current node
        node.isLocked = True
        node.uid = uid

        # Update locked_descendants for all ancestors
        current = node.parent
        while current:
            current.locked_descendants.add(node)
            current = current.parent

        return True

    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, 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

def main():
    n = int(sys.stdin.readline())
    m = int(sys.stdin.readline())
    q = int(sys.stdin.readline())
    name_list = [sys.stdin.readline().strip() for _ in range(n)]
    queries = [sys.stdin.readline().strip() for _ in range(q)]
    tree = Tree(m, n, name_list)
    results = tree.process_queries(queries)
    for res in results:
        print(res)

if __name__ == "__main__":
    main()

"""
"""
import sys

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

def main():
    input = sys.stdin.read
    data = input().split('\n')
    idx = 0
    n = int(data[idx].strip())
    idx += 1
    m = int(data[idx].strip())
    idx += 1
    q = int(data[idx].strip())
    idx += 1
    name_list = []
    for _ in range(n):
        if idx >= len(data):
            break
        name = data[idx].strip()
        name_list.append(name)
        idx += 1
    queries = []
    for _ in range(q):
        if idx >= len(data):
            break
        query = data[idx].strip()
        queries.append(query)
        idx += 1
    tree = Tree(m, n, name_list)
    results = tree.process_queries(queries)
    print('\n'.join(results))

if __name__ == "__main__":
    main()
"""