Empire

Interview

Time limit1sMemory limit256 MB

Summary
Simulate a tree of kingdoms and wars: process each battle in order, transfer vassal subtrees on losses and successful rebellions, then report the root kingdoms sorted by ASCII order.
Level

Medium7 of 10

Topics
Tree, Simulation, Hash map, Implementation
Solved
No attempts yet

Problem

In the 73rd year of the Baeseong calendar, the Seongil Empire, which had dominated the continent, collapsed after an exhausting war of conquest. Rebel forces that had been waiting for an opportunity seized the chaos and each declared a kingdom, and the kingdoms fought countless wars to take the Empire's place.

A war proceeds as follows.

A kingdom that is not a vassal of another kingdom may attack another kingdom that is not its own vassal and wage war. If it wins the war, it takes that kingdom and all of that kingdom's vassals as its own vassals. Sometimes it attacks a vassal of another kingdom, and in this case the overlord of that kingdom (the kingdom that holds it) spares no support in defending it. So if it wins here, it can take even the now bankrupt overlord and its vassals as its own vassals. However, if it loses the war, it and its vassals all pass to the opposing kingdom (or to that kingdom's overlord, if the opposing kingdom is a vassal) as vassals.

A vassal basically cannot attack another kingdom, but there is one exception: attacking its own overlord. If the vassal wins this war, it escapes its vassal status and takes the kingdom that was its overlord and that kingdom's vassals as its vassals. However, if the overlord wins, unfortunately nothing happens.

Given the names of the kingdoms and the results of the wars between pairs of kingdoms, output the number of kingdoms that are not vassals after all wars end, and the names of each of those kingdoms.

Input

The first line gives the number of kingdoms N(2 ≤ N ≤ 500) and the number of war results M(1 ≤ M ≤ 2,000).

Each of the next N lines gives the name of one kingdom.

A kingdom's name always starts with “Kingdom of ”, followed by a single word with no spaces. A kingdom name consists only of uppercase and lowercase alphabet letters and spaces, and its total length including spaces does not exceed 20. Kingdom names are not duplicated.

Each of the next M lines gives the result of a war.

A war result is given as the name of kingdom1, the name of kingdom2, and w(w = 1 or 2), in a format with a comma (,) between them and no spaces, where w = 1 means kingdom1 won and w = 2 means kingdom2 won. The input order of the kingdom names has no relation to which side attacked first, and only wars that can hold under the problem's conditions are given as input.

Output

On the first line, output the number of kingdoms that are not vassals.

Starting from the second line, output the names of the kingdoms that are not vassals, sorted in ASCII lexicographic order, one per line.

Examples3

  1. Example 1

    Input
    5 2
    Kingdom of A
    Kingdom of B
    Kingdom of C
    Kingdom of D
    Kingdom of E
    Kingdom of A,Kingdom of B,1
    Kingdom of C,Kingdom of D,2
    
    Expected output
    3
    Kingdom of A
    Kingdom of D
    Kingdom of E
    
  2. Example 2

    Input
    5 5
    Kingdom of A
    Kingdom of B
    Kingdom of C
    Kingdom of D
    Kingdom of E
    Kingdom of A,Kingdom of B,2
    Kingdom of C,Kingdom of D,1
    Kingdom of E,Kingdom of C,2
    Kingdom of C,Kingdom of D,2
    Kingdom of D,Kingdom of E,2
    
    Expected output
    2
    Kingdom of B
    Kingdom of E
    
  3. Example 3

    Input
    5 4
    Kingdom of A
    Kingdom of B
    Kingdom of C
    Kingdom of D
    Kingdom of E
    Kingdom of A,Kingdom of B,2
    Kingdom of C,Kingdom of D,1
    Kingdom of E,Kingdom of C,2
    Kingdom of A,Kingdom of C,1
    
    Expected output
    1
    Kingdom of B