This page is still under construction.

Parts of this page are still being built. What you see may change.

League of Legeno

Time limit2sMemory limit512 MB

Summary
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 NN (1 ≤ NN ≤ 200,000), the number of item relations Baeknam knows. Across the next NN 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.

Examples3

  1. Example 1

    Input
    4
    galeforce everfrost
    riftmaker everfrost
    goredrinker galeforce
    stridebreaker galeforce
    
    Expected output
    goredrinker
    riftmaker
    stridebreaker
    galeforce
    everfrost
    
  2. Example 2

    Input
    2
    riftmaker galeforce
    galeforce riftmaker
    
    Expected output
    -1
    
  3. Example 3

    Input
    2
    goredrinker galeforce
    riftmaker everfrost
    
    Expected output
    goredrinker
    riftmaker
    everfrost
    galeforce