#SRD0000135. Tối ưu số con sau khi xóa đỉnh (Tree Queries)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tối ưu số con sau khi xóa đỉnh (Tree Queries)
Tree Queries
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected tree rooted at vertex . Every non-root vertex has one parent; vertices immediately below a vertex are its direct children.
There are independent queries. For a query , you may delete vertices in any order, but you must not delete either the root or the queried vertex . Whenever a non-root vertex is deleted, all of its current children become direct children of the current parent of .
After the deletions, let be the number of deleted vertices and the number of direct children of . The score is
Maximize this value for each query. Every query is processed on the original tree; deletions from one query do not carry over to the next.
Input
- The first line contains .
- Each of the next lines contains endpoints of an undirected edge. These edges form a tree.
- The following line contains .
- Each of the next lines contains , the parameters of query .
Output
Print exactly lines. The -th line is the maximum score for query .
Subtasks
- Subtask 1 (10 points): ; an undirected tree has vertices, edges and root ; , ; every query satisfies and .
- Subtask 2 (20 points): ; an undirected tree has vertices, edges and root ; , ; every query satisfies and .
- Subtask 3 (30 points): ; an undirected tree has vertices, edges and root ; , ; every query satisfies and .
- Subtask 4 (40 points): ; an undirected tree has vertices, edges and root ; , ; every query satisfies and .
Examples
Example 1
Input
8
6 7
3 2
8 3
5 7
7 4
7 1
7 3
6
1 0
1 2
1 3
7 1
5 0
7 200000
Output
5
2
1
4
0
4
Explanation
The root has child ; vertex has children , and vertex has children . For , delete and to give root five children and score . For , deleting yields four children at cost , score . For , no deletion gives score . For , deleting gives five children at cost , score . Vertex is a leaf, so query yields . For , deleting nothing leaves four direct children and score .
Example 2
Input
1
3
1 0
1 1
1 200000
Output
0
0
0
Explanation
There is only root vertex , with no children and no deletable vertices. Hence , , and the maximum score is zero for each of the three values of .