League of Legeno
Time limit2sMemory limit512 MB
Given partial precedence relations between items, simulate buying all currently available items in lexicographic order and output the resulting full purchase order, or -1 if impossible.
- Level
Medium7 of 10
- Topics
- Topological sort, Graph, Heap, Simulation
- Solved
- No attempts yet
Problem
Baeknam has started a new semester and picked up a game called League of Legeno. League of Legeno is an AOS (Aeon of Strife) game where five players form a team and the goal is to destroy the opponent's main building. To win, players must raise their character's stats. Killing monsters on the map or players on the opposing team rewards experience and gold, and that experience raises the character's level, which grants stats for each level gained. However, a game has a fixed level cap. Another way to raise stats is to spend gold on items.
There is a predetermined purchase order among the items. Baeknam, who has just started playing, knows only part of the precedence relations between pairs of items, not the whole purchase order. Given that Baeknam buys items by repeating the following process, determine the full purchase order of the items.
- Find every item that can currently be purchased and has not been purchased yet.
- Buy all of the found items in lexicographic order.
\
Input
The first line gives (1 ≤ ≤ 200,000), the number of item relations Baeknam knows. Across the next lines, two strings A B are given, each the name of an item. Item A must be purchased before item B can be purchased, and A and B are always different. Every item appears at least once in the precedence relations. Item names consist only of lowercase English letters and contain no spaces. The length of an item name is between 1 and 15.
Output
Print the items in order, starting with the item that must be purchased first, one per line. If not all items can be purchased, print -1.