아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Horse Race

면접 대비

시간 제한0.1초메모리 제한1024 MB

요약
각 소규모 경주가 전체 경주에서의 결승 순위로 우승마를 알려줄 때, R개의 우승 조건을 모두 만족하는 N마리의 전체 순서를 복원한다.
난이도

보통10점 중 6점

유형
그래프, 위상 정렬, 정렬, 구현
정답자
아직 제출이 없습니다

문제

The Exponential Horse Racing company has been building larger and larger stadiums, allowing for many more horses than usual to participate in the same race. And, to facilitate filling those slots, it combines races of different tiers. That is, horses that are known to be much worse than others are allowed to race together, and spectators are allowed to bet on each of the small races that are happening. This also allows for horses that are between tiers to race in both tiers at once.

Paul is in charge of noting down the winner of each small race. To speed up that work, instead of noting down the full name of the winning horse, Paul notes down only its placement on the full race.

As an example, suppose that horses “a”, “b”, “c”, “d” and “e” participated in a full race, and they reached the finish line in the order “e”, “c”, “a”, “b” and “d”. Hence a small race with horses “c” and “b” was won by “c”, and then Paul would note down that the winner of the small race was the second horse, because “c” got that placement on the full race.

One day you got Paul’s notes, but didn’t have the full race results on hand. All you know is that there were no ties in the full race, and that every horse reached the finish line. Can you figure out the full race results by having only the description of each small race?

입력

The first line contains an integer NN (2≤N≤3002 ≤ N ≤ 300) indicating the number of horses in the full race. The second line contains NN different strings representing the names of the horses. Each string has a length of up to three and is made of lowercase letters. The third line contains an integer RR (1≤R≤1051 ≤ R ≤ 10^5) denoting the number of small races. For i=1,2,…,Ri = 1, 2, \dots , R, the ii-th of the next RR lines describes a small race with two integers M_iM\_i (2≤M_i≤N2 ≤ M\_i ≤ N) and W_iW\_i (1≤W_i≤N1 ≤ W\_i ≤ N), followed by M_iM\_i different strings, indicating respectively the number of horses in the small race, the winner according to Paul’s notes, and the names of the participating horses. It is guaranteed that ∑_iM_i≤105\sum\_i{M\_i} ≤ 10^5.

출력

Output a single line with NN strings indicating the names of the horses in a valid order, that represents a possible result of the full race. It is guaranteed that there exists at least one solution. If there are multiple solutions, output any of them.

예제2

  1. 예제 1

    입력
    5
    a b c d e
    3
    4 2 a b c d
    2 4 b d
    2 2 c b
    
    예상 출력
    e c a b d
    
  2. 예제 2

    입력
    2
    aaa b
    3
    2 1 aaa b
    2 1 b aaa
    2 1 aaa b
    
    예상 출력
    b aaa