Paper Pile Pandemonium

시간 제한1초메모리 제한2048 MB

요약
번호가 붙은 종이 더미의 초기 상태와, 한 더미 위에서 다른 더미 위로 종이 묶음을 옮기는 순서가 주어질 때, 모든 이동이 끝난 뒤 각 더미의 내용을 출력한다.
난이도

보통10점 중 4점

유형
시뮬레이션, 스택, 구현
정답자
아직 제출이 없습니다

문제

It's nearing the end of a busy semester, and you have piles of homework to finish. Literally. All of your homework was printed on sheets of paper (poor trees), which you laid out on your desk in a series of piles. As you were completing the assignments, you moved sheets of paper from the top of one pile to the top of another pile, and kept a diligent log as you did so to keep track of which assignments were completed.

Suddenly, you realize that some of your assignments are due in only in a few minutes, and you need to scan and turn them in now! Through all the shuffling you unfortunately lost track of where all the assignments are amongst the piles. However, your diligent log should let you determine where the assignments must be now. Can you write a program that can determine where all the assignments are now located?

입력

The first line of input contains two space-separated integers, 1≤N≤1,0001 \leq N \leq 1\\,000 and 1≤P≤1,0001 \leq P \leq 1\\,000, the number of sheets of paper, and the number of piles the sheets of paper are organized in, respectively. The next PP lines each begin with a single integer 0≤p_i≤N0 \leq p\_i \leq N, indicating the number of sheets of paper that were initially in the ithi^{\text{th}} pile. That integer is followed by a space and then p_ip\_i space-separated integers in the range \[1,N]\[1, N] denoting, from left to right, the sheet numbers that appear in the pile from bottom to top. Note that sheet numbers will appear exactly once amongst all the piles, and that ∑_i=1Pp_i=N\sum\_{i=1}^{P} p\_i = N (the sum of sheets in each pile is equal to the total number of sheets, NN).

The next line contains a single integer, 0≤M≤1,0000 \leq M \leq 1\\,000, the number of times sheets of paper were moved from one pile to another. The next MM lines each contain three space separated integers defining one such movement, 1≤s_i≤P1 \leq s\_i \leq P, 1≤d_i≤P1 \leq d\_i \leq P, and 1≤n_i≤N1 \leq n\_i \leq N, the source pile number, the destination pile number, and the number of sheets of paper that were moved from the top of the source pile to the top of the destination pile, respectively. When the sheets are moved, they are lifted as a group off the top of the source pile, and placed on top of the destination pile without changing their order. Note also that for all ii, s_i≠d_is\_i \neq d\_i, and it is guaranteed that number of sheets in pile s_is\_i before the ithi^{\text{th}} movement is greater than or equal to n_in\_i.

출력

You should output PP lines, with the ithi^\text{th} line containing zero or more space-separated integers denoting, from left to right, the sheet numbers that appear in the ithi^\text{th} pile (from bottom to top) after all movements have occurred. If there are no sheets in a pile, output an empty line.

예제2

  1. 예제 1

    입력
    6 3
    3 1 2 3
    2 4 5
    1 6
    3
    1 3 2
    2 1 1
    2 3 1
    
    예상 출력
    1 5
    
    6 2 3 4
    
  2. 예제 2

    입력
    20 5
    6 6 19 1 20 13 3
    2 7 4
    3 15 17 11
    7 16 18 10 2 12 5 9
    2 8 14
    10
    1 2 5
    2 5 3
    4 2 2
    2 5 5
    4 3 1
    5 4 9
    3 5 3
    1 3 1
    4 1 6
    5 2 3
    
    예상 출력
    3 4 19 1 5 9
    7 17 11 12
    15 6
    16 18 10 2 14 20 13
    8