No.1153 Cat Game
タグ : / 解いたユーザー数 45
作問者 :
Kiri8128
/ テスター :
tarattata1
Problem Statement
Ebi-chan and Kiri-chan play the cat game.
The cat game is a game in which cats are moved on a tree.
The tree has $N$ vertices and $N-1$ edges. The vertices are numbered from $1$ to $N$, and
the $i\ (1\le i \le N-1)$-th edge connects vertex $u_i$ and vertex $v_i$.
There are $M$ cats, and initially the $i\ (1\le i\le M)$-th cat is at vertex $A_i$.
Starting with Ebi-chan, they alternately perform the following operation.
- Choose one cat and move it to an adjacent vertex. However, it cannot be moved to a vertex where that cat has been before (because the cat would get bored).
The player who can no longer perform the operation loses, and the other player wins.
It can be shown that exactly one of Ebi-chan and Kiri-chan has a winning strategy (a way to always win regardless of the opponent's actions). If Ebi-chan has a winning strategy, output one of the best first moves for Ebi-chan (a move that maintains a state with a winning strategy). Otherwise, report that fact.
Input
$N\ M$
$A_1\ A_2\ \cdots\ A_M$
$u_1\ v_1$
$u_2\ v_2$
$\vdots$
$u_{N-1}\ v_{N-1}$
All inputs are integers.
$1 \le N \le 2 \times 10^5$
$1 \le M \le 2 \times 10^5$
$1 \le u_i,\ v_i\le N\ (1\le i\le N-1)$
$u_i\neq v_i\ (1\le i\le N-1)$
$1 \le A_i\le N\ (1\le i\le M)$
The given graph is guaranteed to be a tree.
Output
$i\ v$
$i$ and $v$ are integers and must satisfy either "$1\le i\le M$ and $1\le v\le N$" or $i=v=-1$.
The meaning of the output is as follows.
If Ebi-chan has a winning strategy
Output one of the best first moves.
If the move takes the $i$-th cat to vertex $v$, output $i$ and $v$ separated by a space.
If there are multiple best moves, you may output any of them.
If Kiri-chan has a winning strategy
Let $i=v=-1$ and output "-1 -1".
Print a newline at the end.
Sample
Sample 1
Input
6 1 3 1 2 2 3 3 4 4 5 5 6
Output
1 4
There is one cat, which is initially at vertex 3.
In this case, Ebi-chan has a winning strategy, and moving the 1st cat to vertex 4 on the first turn is the best move, so output 1 and 4 separated by a space.
The subsequent actions of the two players are as follows.
- Ebi-chan moves the 1st cat to vertex 4 on the first turn. (She can move it to vertex 2 or vertex 4, but moving it to vertex 4 is the best move.)
- Kiri-chan moves the 1st cat to vertex 5. The cat cannot be moved to vertex 3 because the 1st cat has been there before.
- Ebi-chan moves the 1st cat to vertex 6. The cat cannot be moved to vertex 4 because the 1st cat has been there before.
Sample 2
Input
1 2 1 1
Output
-1 -1
There are two cats, but neither of them can be moved.
Note that multiple cats may be at the same vertex.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。