#P16464. [UOI 2026] Game on a Tree

[UOI 2026] Game on a Tree

题目描述

You are given a tree with nn vertices. Each leaf (that is, a vertex of degree 11) has a status: active\textbf{active} or inactive\textbf{inactive}. Initially, all leaves are inactive.

There are qq queries of two types:

  • 1 v\textbf{1 v} --- change the status of leaf vv to the opposite one (active \leftrightarrow inactive). It is guaranteed that vv is a leaf (a vertex of degree 11).
  • 2 s\textbf{2 s} --- determine who wins the game described below, if the token is initially placed at vertex ss. It is guaranteed that ss is not\textbf{not} a leaf (a vertex of degree 2\ge 2).

Game:\textbf{Game:} there is a token initially placed at vertex ss. Two players take turns, with the first player moving first. On each move, a player moves the token to an unvisited adjacent vertex. The game ends when the token is at a vertex from which it cannot move anywhere (that is, all adjacent vertices have already been visited --- this is necessarily a leaf of the graph). The second player wins if the token ends its move at an active\textbf{active} vertex; otherwise, the first player wins.

Online mode

If the input parameter m=1m = 1, then in each query the actual vertex number is replaced by an encrypted number.

Consider a query of one of the two types:

  • 1 x\texttt{1 x} --- an encrypted query to change the status of a leaf;
  • 2 x\texttt{2 x} --- an encrypted query for a game from some starting vertex.

Let acc\mathit{acc} be a counter, initially equal to 00. For each query, the number xx is decrypted into the actual vertex number vv by the formula

v=((x1+acc)modn)+1.v = ((x - 1 + \mathit{acc}) \bmod n) + 1.

That is:

  • in query 1 x\texttt{1 x}, you need to change the status of leaf vv;
  • in query 2 x\texttt{2 x}, the game starts at vertex vv.

After each query, the counter is updated:

  • after query 1 x\texttt{1 x} with decrypted leaf vv: acc=(acc+v)modn\mathit{acc} = (\mathit{acc} + v) \bmod n;
  • after query 2 x\texttt{2 x} with answer w{1,2}w \in \{1, 2\}: acc=(acc+w)modn\mathit{acc} = (\mathit{acc} + w) \bmod n.

If m=0m = 0, then the queries are not encrypted. In that case, in query 1 v\texttt{1 v} the number vv is the leaf whose status must be changed, and in query 2 v\texttt{2 v} the number vv is the starting vertex of the game.

输入格式

The first line contains three integers nn, qq, and mm (3n51053 \le n \le 5 \cdot 10^5, 1q51051 \le q \le 5 \cdot 10^5, m{0,1}m \in \{0, 1\}).

The next n1n - 1 lines contain the edges of the tree: two integers uiu_i and viv_i each (1ui,vin1 \le u_i, v_i \le n, uiviu_i \ne v_i).

The next qq lines contain the queries: type t{1,2}t \in \{1, 2\} and argument xx (1xn1 \le x \le n). If m=1m = 1, then xx is an encrypted value.

输出格式

For each query of the second type, output one number in a separate line: 11 if the first player wins, or 22 if the second player wins.

4 5 0
1 2
1 3
1 4
2 1
1 2
1 3
1 4
2 1
1
2
5 5 0
1 2
1 3
3 4
3 5
2 3
1 2
1 4
1 5
2 3
1
2
4 5 1
1 2
1 3
1 4
2 1
1 1
1 4
1 2
2 3
1
2

提示

In the first example, the tree is a star with vertex 11 in the center and leaves 2,3,42, 3, 4. Initially all leaves are inactive: for query 2 1\texttt{2 1}, the first player makes the only move to any leaf, and the token stops at an inactive leaf --- the first player wins, so the answer is 11. After three queries of type 1\texttt{1} that activate all leaves, for the repeated query 2 1\texttt{2 1}, no matter where the first player moves, the token ends at an active leaf --- the second player wins, so the answer is 22.

In the second example, the tree has a Y\textit{Y} shape: vertex 33 is connected to vertices 44, 55, and to vertex 11, which has leaf 22 attached to it. Query 2 3\texttt{2 3} means that the tree is rooted at vertex 33. Before activating any leaves, the first player can reach an inactive leaf from vertex 33 in one move (for example, to 55) --- answer 11. After activating leaves 22, 44, and 55, all three possible paths from 33 end at an active leaf, so the first player cannot avoid losing --- answer 22.

The third example demonstrates the online mode (m=1m = 1) on the same tree as in the first example. The initial value is acc=0\mathit{acc} = 0, so the first encrypted query 2 1\texttt{2 1} is decoded as 2 1\texttt{2 1} and gives answer 11, after which acc=1\mathit{acc} = 1. The next encrypted query 1 1\texttt{1 1} is decoded as 1 2\texttt{1 2} (because ((11+1)mod4)+1=2((1 - 1 + 1) \bmod 4) + 1 = 2), activating leaf 22, and then acc=(1+2)mod4=3\mathit{acc} = (1 + 2) \bmod 4 = 3. Similarly, 1 4\texttt{1 4} is decoded as 1 3\texttt{1 3} (activating leaf 33, acc=2\mathit{acc} = 2), and 1 2\texttt{1 2} --- as 1 4\texttt{1 4} (activating leaf 44, acc=2\mathit{acc} = 2). The last encrypted query 2 3\texttt{2 3} is decoded as 2 1\texttt{2 1} and returns 22.

Scoring

  • (44 points): n,q1000n, q \le 1000, m=0m = 0;
  • (88 points): a leaf never changes its status from active to inactive, s=1s = 1, m=0m = 0;
  • (1313 points): a leaf never changes its status from active to inactive, the token is initially at vertex 11 in every query, m=1m = 1;
  • (2121 points): m=0m = 0, there exists an integer k1k \ge 1 such that n=2k1n = 2^k - 1, and the tree edges are of the form (i,i/2)(i, \lfloor i / 2 \rfloor) for all 2in2 \le i \le n.
  • (88 points): only vertex 11 has degree greater than 22, m=0m = 0;
  • (1919 points): s=1s = 1, m=0m = 0;
  • (1919 points): n105n \le 10^5, m=0m = 0;
  • (88 points): no additional constraints.