#P10039. Tree Game

    ID: 12017 Type: Default 1000ms 256MiB

Tree Game

Tree Game

Players I and J play a game on a tree with n nodes. The tree's edges are in one of two states: open or closed. Initially, every edge is open. A chess piece is placed at node 1. Player I can move the chess piece along any open edge incident to the current node. The goal of Player I is to eventually have the chess piece located on a node whose open degree (i.e. the number of open edges incident to it) is exactly 1. Before each move of Player I the following sequence happens:

  1. Task Check: If the chess piece is on a node whose open degree is exactly 1 (note that if an edge had been removed in earlier rounds, the open degree might be lower than the original degree), then Player I wins immediately.
  2. Player J's action: Player J chooses any open edge in the tree and permanently closes it. (If no open edge exists, this step is skipped.)
  3. Player I's move: Player I must choose one open edge incident to the current node (the one currently holding the chess piece) and move the piece along it to the adjacent node. If no open edge is available, Player J wins.

Both players know the entire structure of the tree and play optimally. It can be shown that except for one trivial case, Player I always has a winning strategy.

Analysis: Observe that if n = 1 then the chess piece starts on the only node, whose open degree is 0 (since there are no edges) and it is not considered to meet the win condition (which requires an open degree of exactly 1). In that case, Player I cannot move and thus Player J wins. For any tree with n > 1, the starting node (node 1) has an open degree of at least 1. With optimal play, Player I can always force the situation to eventually arrive at a node with open degree exactly 1. Therefore, the answer is:

  • If n = 1: Player J wins (output: J).
  • If n > 1: Player I wins (output: I).

In mathematical terms, the answer can be summarized as:

$$answer = \begin{cases} J, & n = 1, \\ I, & n > 1. \end{cases} $$

inputFormat

The input begins with a single integer n (1 ≤ n ≤ 10^5), the number of nodes in the tree. Each of the following n - 1 lines contains two integers u and v (1 ≤ u, v ≤ n), representing an undirected edge between nodes u and v. It is guaranteed that the given edges form a tree.

outputFormat

Output a single character: I if Player I wins under optimal play, or J if Player J wins.

sample

1
J