PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#DSU0000092. Giải đấu hiệp sĩ (Knight Tournament)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Giải đấu hiệp sĩ (Knight Tournament)
Knight Tournament
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
There are knights numbered from to , all initially active. In each of the battles, every still-active knight whose number lies in participates. Knight wins and remains active; every other participating knight is eliminated. Each battle has at least two active participants, and the sequence is guaranteed valid. Determine the knight who eliminated each participant; the overall champion is never eliminated.
Input
The first line contains . Each following line contains .
Output
Print integers: the -th is the index of the knight who eliminated knight , or for the champion.
Subtasks
- Subtask 1 (20 points): , , , ; at least two active knights participate per battle.
- Subtask 2 (30 points): , , , ; at least two active knights participate per battle.
- Subtask 3 (50 points): , , , ; all battles satisfy the guarantees.
Examples
Input
4 3
1 2 1
1 3 3
1 4 4
Output
3 1 4 0
Explanation
Knight loses to , then loses to , and finally loses to .