• + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    from collections import deque
    
    #
    # Complete the 'swapNodes' function below.
    #
    # The function is expected to return a 2D_INTEGER_ARRAY.
    # The function accepts following parameters:
    #  1. 2D_INTEGER_ARRAY indexes
    #  2. INTEGER_ARRAY queries
    #
    
    
    
    # very compact when to depict a tree
    # use a list (or dic) of the nodes to represent the tree
    # the ith node on the tree has the data of i+1
    # so as long as you can find a node that is not -1 then you can find it child and kkep on 
    
    sys.setrecursionlimit(2000)
    # get the inorder from the i in tree
    def inOrder(i, tree):
        # nothing to further check
        if i == -1:
            return []
        # get two sons for this non null node
        # note, its sons are in the i-1 position
        left, right = tree[i-1]
    
        return inOrder(left, tree) + [i] + inOrder(right, tree)
    
    
    # make a list of list to track the depth (use the deque)
    # 0: [1], 1:[2,3], can cause a blank list 
    def getNodeDeepth(tree):
        depth_list = [[1]]
        # the initial queue
        depth_queue = deque([1])
        
        # those are in the same depth
        while depth_queue:
            # we are dealing a new depth of node
            depth_list.append([])
            for _ in range(len(depth_queue)):
                i = depth_queue.popleft()
                l,r = tree[i-1]
                if l != -1:
                    depth_queue.append(l)
                    depth_list[-1].append(l)
                if r != -1:
                    depth_queue.append(r)
                    depth_list[-1].append(r)
            if not depth_list[-1]:
                depth_list.pop()
        return depth_list     
                
            
    
    
    
    def swapNodes(indexes, queries):
        #print(inOrder(1,indexes))
        depth_list = getNodeDeepth(indexes)
        
        ret = []
        for k in queries:
            i = 1
            while i * k <= len(depth_list):
                parents = depth_list[i * k -1]
                for p in parents:
                    l,r = indexes[p -1]
                    indexes[p -1] = r,l
                i += 1
            ret.append(inOrder(1,indexes))
        
        return ret
        # Write your code here
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        n = int(input().strip())
    
        indexes = []
    
        for _ in range(n):
            indexes.append(list(map(int, input().rstrip().split())))
    
        queries_count = int(input().strip())
    
        queries = []
    
        for _ in range(queries_count):
            queries_item = int(input().strip())
            queries.append(queries_item)
    
        result = swapNodes(indexes, queries)
    
        fptr.write('\n'.join([' '.join(map(str, x)) for x in result]))
        fptr.write('\n')
    
        fptr.close()