This page is still under construction.

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

Teams that can win

Time limit1sMemory limit128 MB

Summary
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.
Level

Medium7 of 10

Topics
Graph, Tree, DFS, Greedy
Solved
No attempts yet

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 nn teams needs exactly n−1n - 1 games before a winner is decided, and the sheikh's list holds exactly n−1n - 1 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 nn (2≤n≤10002 \le n \le 1000), the number of teams participating in the tournament. The next nn 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 n−1n - 1 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.

Examples1

  1. Example 1

    Input
    3
    A
    B
    C
    A B
    B C
    5
    A
    B
    C
    D
    E
    A B
    C D
    A E
    C E
    0
    
    Expected output
    3 A
    3 A