問題一覧 > 通常問題

No.1153 Cat Game

レベル : / AI推定難易度 : 2025 / 実行時間制限 : 1ケース 2.500秒 / メモリ制限 : 512 MB / スペシャルジャッジ問題 (複数の解が存在する可能性があります)
タグ : (AC するまで非表示) / 解いたユーザー数 45
作問者 : Kiri8128 / テスター : tarattata1
お気に入りにしたユーザー ProblemId : 4619 / 自分の提出
問題文最終更新日: 2020-08-06 23:09:34
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 260 (Typical Game Contest) (順位表) の他の問題:

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).
Multiple cats are allowed to be at the same vertex.
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.
Kiri-chan cannot move any cat any further, so Ebi-chan wins.

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もしくは右上の雲マークをクリックしてアカウントを作成してください。