Valentine's Day '19 S5 - A Top Tree Problem

View as PDF

Submit solution


Points: 25 (partial)
Time limit: 2.5s
Java 3.0s
Memory limit: 512M

Author:
Problem types

Jonathan is making a connected tree of N nodes, and N-1 bidirectional edges. He wants to give the tree to <REDACTED> for Valentine's Day. However, he wants to make sure the tree is good looking.

Jonathan defines a simple path in the tree as a path where every node is traversed at most once.

To determine whether the tree is good looking, he will give you Q queries that you must answer for him:

  • v k u_1 u_2 ... u_k Is there a simple path that visits u_1, u_2, \ldots, u_k in that order, if at most v bidirectional edges are added into the tree?

3 \le k \le \min(N, 20), 0 \le v \le 2. u_i is guaranteed to be distinct.

Input Specification

The first line will contain two integers, N, Q\ (3 \le N \le 10^5, 1 \le Q \le 2 \times 10^4).

The next N-1 lines will each contain two integers a, b\ (1 \le a, b \le N), indicating that there is a bidirectional edge between a and b. It is guaranteed that the tree is connected.

The next Q lines will each contain a query as defined above.

Output Specification

For each query, output 1 if the answer to the query is yes, and 0 otherwise.

Subtasks

Subtask 1 [5%]

N, Q \le 100, v = 0

Subtask 2 [25%]

v = 0

Subtask 3 [70%]

No further constraints.

Sample Input 1

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

Sample Output 1

1
0
0

Sample Input 2

7 5
1 2
2 3
2 4
4 5
5 6
5 7
0 3 3 4 7
0 4 2 5 7 6
1 4 2 5 7 6
2 5 3 4 2 1 7
1 6 6 5 7 4 1 3

Sample Output 2

1
0
1
1
0

Comments

There are no comments at the moment.