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

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

Redistributing Gifts

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

요약
각 소가 N개의 선물에 대해 선호 순위를 가지고 있을 때, 어떤 소도 원래보다 나빠지지 않는 재분배에서 각 소가 받을 수 있는 가장 선호하는 선물을 구한다.
난이도

보통10점 중 7점

유형
그래프, DFS, 그리디
정답자
아직 제출이 없습니다

문제

Farmer John has NN gifts labeled 1…N1\ldots N for his NN cows, also labeled 1…N1\ldots N (1≤N≤5001\le N\le 500). Each cow has a wishlist, which is a permutation of all NN gifts such that the cow prefers gifts that appear earlier in the list over gifts that appear later in the list.

FJ was lazy and just assigned gift ii to cow ii for all ii. Now, the cows have gathered amongst themselves and decided to reassign the gifts such that after reassignment, every cow ends up with the same gift as she did originally, or a gift that she prefers over the one she was originally assigned.

For each ii from 11 to NN, compute the most preferred gift cow ii could hope to receive after reassignment.

입력

The first line contains NN. The next NN lines each contain the preference list of a cow. It is guaranteed that each line forms a permutation of 1…N1\dots N.

출력

Please output NN lines, the ii-th of which contains the most preferred gift cow ii could hope to receive after reassignment.

힌트

In this example, there are two possible reassignments:

  • The original assignment: cow 11 receives gift 11, cow 22 receives gift 22, cow 33 receives gift 33, and cow 44 receives gift 44.
  • Cow 11 receives gift 11, cow 22 receives gift 33, cow 33 receives gift 22, and cow 44 receives gift 44.

Observe that both cows 11 and 44 cannot hope to receive better gifts than they were originally assigned. However, both cows 22 and 33 can.

예제1

  1. 예제 1

    입력
    4
    1 2 3 4
    1 3 2 4
    1 2 3 4
    1 2 3 4
    
    예상 출력
    1
    3
    2
    4