Seongho and Dusan
Time limit1sMemory limit1024 MB
Players alternately remove leaves from their own trees, and the removed marble's color picks the next player. Find the min and max marbles left at the end.
- Level
Hard8 of 10
- Topics
- Tree, Greedy, Game theory
- Solved
- No attempts yet
Problem
There are red marbles and blue marbles. Seongho and Dusan play a game with these marbles. Before the game starts, each of them takes marbles. Each player builds a tree whose vertices are the marbles they took, and picks one marble as the root. That marble becomes marble 1.
The game starts with Seongho. On each turn, the current player does the following.
- Removes one leaf marble from their own tree. If their tree has no marble left to remove, the game ends.
- The next turn belongs to Seongho if the removed marble was red, and to Dusan if it was blue. Repeat this process.
Input
The first line contains the natural number .
The second line contains integers separated by spaces, the colors of the marbles Seongho took. The -th integer is the color of marble , where means red and means blue. ()
The next lines, starting from the third line, each contain two marble numbers separated by a space. These are the edges of the tree Seongho built.
Line contains integers separated by spaces, the colors of the marbles Dusan took, in the same format as Seongho's colors.
The next lines, starting from line , each contain two marble numbers separated by a space. These are the edges of the tree Dusan built.
Output
Print the minimum and the maximum possible number of marbles remaining in the whole game when it ends, separated by a space.
Constraints
- There are red marbles and blue marbles in total.
- Each player's marble numbers run from 1 to , and they are distinct from each other.
- The edges each player connected form a tree.