#P17440. 水滴

水滴

Problem Description

Jiu and JiuZhu are playing a game.

There is a tree rooted at 11, and there is one drop of water on the tree. Jiu and JiuZhu take turns to move, with Jiu going first. In each move, you may choose a node on the tree that has a water drop, choose some of its children (you cannot choose none), gently remove the drop from this node, split it into several drops, and then gently place them onto the chosen children. It is easy to see that leaves cannot be operated on. When a player cannot make a move, she loses.

They like this game very much, so they plan to play many rounds. In each round, they agree on rtrt and dd, then wipe all water off the tree, and place exactly one drop of water only on rtrt. Next they start the game. During the game, water drops may only appear at nodes whose distance to rtrt is ≤d\le d. Here, the distance between two nodes is the number of edges on the simple path between them. For each round, Jiu wants to know whether she has a winning strategy.

Formally, given a rooted tree TT with root 11, define the set of children of node xx as sonxson_x. For each game round (rt,d)(rt,d), the set of operable nodes is S={x∣dis(x,rt)≤d}S=\{x|\text{dis}(x,rt)\le d\}. Let AA be the set of nodes that currently have water drops. Initially, A={rt}A=\{rt\}. Then, each move may choose a node xx such that x∈Ax\in A, and choose a non-empty set S′S' such that S′⊆sonx∩SS' \subseteq son_x \cap S, delete xx from AA and add all elements of S′S' into AA. The player who cannot move loses. Determine whether the first player has a winning strategy.

Input Format

The first line contains two positive integers n,qn,q, representing the size of the rooted tree and the number of game rounds.

The next n−1n-1 lines each contain two positive integers u,vu,v, representing an edge of the rooted tree.

The next qq lines each contain two non-negative integers rt,drt,d, representing rtrt and dd in one game round.

Output Format

Output qq lines. The ii-th line represents the result of the ii-th game round: if Jiu has a winning strategy, output TAK, otherwise output NIE.

8 4
1 2
1 3
2 4
3 5
3 8
5 6
5 7
1 4
1 2
5 2
5 1
NIE
TAK
TAK
TAK

Hint

For all data, 1≤n,q≤1×1061\le n,q\le 1\times 10^6, 1≤u,v,rt≤n1\le u,v,rt\le n, 0≤d≤n0\le d \le n. The test points are evenly distributed.

Test Point ID n q Special Property
1∼21\sim 2 ≤20\le 20 The tree is guaranteed random.
3∼43\sim 4 ≤2000\le 2000
5∼65\sim 6 ≤1×106\le 1\times 10^6 ≤20\le 20
7∼97\sim 9 ≤1×106\le 1\times 10^6 The tree is guaranteed random.
10∼1210\sim 12 The tree is guaranteed to have a special shape.
13∼1613\sim 16 ≤2×105\le 2\times 10^5
17∼1817\sim 18 ≤5×105\le 5\times 10^5
19∼2019\sim 20 ≤1×106\le 1\times 10^6

The random tree is generated as follows: for each ii with 1<i≤n1<i\le n, its parent is chosen uniformly at random among the integers in [1,i−1][1,i-1].

The special shape of the tree is as follows: with root 11, the parent of 2i+12i+1 is 2i−12i-1, and the parent of 2i2i is either 11 or 2i−12i-1.

Note: the maximum input size is about 2424 MB, and the maximum output size is about 4.764.76 MB, so please use fast input/output. The provided file includes fastio.cpp, which you may use.

Time limit: 22s. Memory limit: 10241024 MB.

Translated by ChatGPT 5