Teams that can win
Time limit1sMemory limit128 MB
Given n teams and n-1 desired games, count the teams that can be champion under some valid single-elimination schedule that plays every listed game.
Problem
Sheikh Abdul really loves football. You had better not ask how much money he spent to bring famous teams to the tournament he holds every year. Having spent that much, he wants to see certain teams play each other, so he wrote down a complete list of the games he wants to watch.
These games are distributed into rounds under the following rules.
- In each round, every remaining team plays at most one game.
- If the number of remaining teams is even, every team plays exactly one game.
- If the number of remaining teams is odd, exactly one team plays no game. That team advances to the next round with a wildcard.
- The winner of each game advances to the next round, and the loser is eliminated from the tournament.
- Once a single team is left, that team is the winner of the tournament.
Induction shows that a tournament with teams needs exactly games before a winner is decided, and the sheikh's list holds exactly games.
After round 1, a team that still has an unplayed game on the list may already be out. A schedule therefore has to fix the winner of every game as well. Different winners give different legal schedules, and the champion changes with them.
Call a team a contender if at least one schedule plays every game on the list, obeys the rules, and ends with that team as the winner. Find how many contenders there are, and which of them has the smallest name.
Input
The input contains several test cases. Each test case starts with an integer (), the number of teams participating in the tournament. The next lines hold the names of the participating teams, one per line. A team name consists of at most 25 letters of the English alphabet ('a' to 'z' or 'A' to 'Z'), and the names inside one test case are distinct.
Then follow lines describing the games the sheikh would like to see, in any order. Each line holds the names of the two teams that take part in that game. The given games always admit a tournament schedule that obeys the rules.
The last test case is followed by a zero.
Output
For each test case, print one line with two values separated by a space. Print the number of contenders first, then the smallest name among the contenders.
Names are compared in ASCII order, so every uppercase letter comes before every lowercase letter and Zulu comes before apple.