Reality
Time limit2sMemory limit512 MB
Choose which contestants to eliminate so that the number of jurors whose two wishes both come true is as large as possible.
- Level
Medium7 of 10
- Topics
- Graph
- Solved
- No attempts yet
Problem
Život na kavi is a popular reality show in which members of parliament compete. The contestants are split into a red team and a black team, and each week a set number of contestants is eliminated from the show.
The jury decides who leaves. Each jury member writes on a separate sheet of paper the name of exactly one contestant who should stay in the show, and the name of exactly one contestant who should leave. The jurors are corrupt, so the two names on the same sheet never belong to members of the same team.
A jury member is happy if both of their wishes come true: the contestant they wanted to stay is still in the show at the end of the week, and the contestant they wanted eliminated is eliminated. Choose the set of contestants to eliminate so that the number of happy jury members is as large as possible.
Input
The first line contains a natural number (), the number of members of the red team. The next lines each contain the name of one red team member.
The next line contains a natural number (), the number of members of the black team. The next lines each contain the name of one black team member.
The next line contains a natural number (), the number of jury members. The next lines each contain two names separated by a space. The first name is the contestant the jury member wants to stay, and the second is the contestant they want to leave.
All names are distinct and consist of at most 10 lowercase English letters.
Output
On the first line, print the number of contestants who leave the show. On each of the next lines, print the name of one leaving contestant, in any order.