The Ashfall Guild keeps a ledger of one-on-one sparring bouts fought during its open tournament. Each ledger entry records the numeric ID of the fighter who won the bout and the numeric ID of the fighter who lost it. No two ledger entries record the exact same (winner, loser) ordered pair twice, but the same fighter may lose to different opponents across different entries, and any fighter's overall win/loss record can otherwise look like anything.
Considering only fighters who fought in at least one recorded bout, report every fighter who has never lost a single bout, and separately, every fighter who has lost exactly one bout.
m, the number of ledger entries.m lines contains two integers w and l: the ID of that bout's winner and the ID of its loser.Two lines:
Example 1
Input
5 10 20 20 30 40 30 40 50 60 50
Expected
10 40 60 20
Explanation
Fighter 20 lost once (to 10, in the first bout) -- that is its only loss, so 20 belongs on the exactly-one-loss list. Fighter 30 lost twice (to 20 and to 40), and fighter 50 lost twice (to 40 and to 60), so both are excluded from both lists. Fighters 10, 40, and 60 never appear as a loser in any bout, so they go on the zero-losses list, printed in increasing order as "10 40 60".
Example 2
Input
1 5 9
Expected
5 9
Explanation
There is only one bout: fighter 5 beat fighter 9. Fighter 5 has zero losses (it never lost), and fighter 9 has exactly one loss (it lost that single bout), so the output is "5" on the first line and "9" on the second.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →