Under construction

Still being built. Things may change

Seongho and Dusan

Time limit1sMemory limit1024 MB

Summary
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 NN red marbles and NN blue marbles. Seongho and Dusan play a game with these marbles. Before the game starts, each of them takes NN 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 NN.

The second line contains NN integers separated by spaces, the colors of the marbles Seongho took. The ii-th integer is the color of marble ii, where 00 means red and 11 means blue. (1≤i≤N1 \leq i \leq N)

The next N−1N-1 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 N+2N+2 contains NN integers separated by spaces, the colors of the marbles Dusan took, in the same format as Seongho's colors.

The next N−1N-1 lines, starting from line N+3N+3, 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

  • 1≤N≤100,0001 \leq N \leq 100,000
  • There are NN red marbles and NN blue marbles in total.
  • Each player's marble numbers run from 1 to NN, and they are distinct from each other.
  • The edges each player connected form a tree.

Examples2

  1. Example 1

    Input
    4
    0 1 0 1
    1 2
    1 3
    4 2
    1 0 0 1
    3 2
    1 4
    2 1
    
    Expected output
    1 2
    
  2. Example 2

    Input
    12
    1 1 0 0 1 0 1 0 1 0 0 1
    1 2
    1 3
    2 4
    2 5
    2 6
    5 9
    5 10
    7 11
    7 12
    3 7
    3 8
    1 1 1 0 1 1 0 1 0 0 0 0
    1 2
    1 3
    2 4
    2 5
    2 6
    5 9
    5 10
    7 11
    3 7
    3 8
    7 12
    
    Expected output
    2 6