PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000135. Tối ưu số con sau khi xóa đỉnh (Tree Queries)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 11. Every non-root vertex has one parent; vertices immediately below a vertex are its direct children.

There are qq independent queries. For a query (v,k)(v,k), you may delete vertices in any order, but you must not delete either the root 11 or the queried vertex vv. Whenever a non-root vertex uu is deleted, all of its current children become direct children of the current parent of uu.

After the deletions, let mm be the number of deleted vertices and c(v)c(v) the number of direct children of vv. The score is

c(v)−m⋅k.c(v)-m\cdot k.

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 nn.
  • Each of the next n−1n-1 lines contains endpoints xi,yix_i,y_i of an undirected edge. These edges form a tree.
  • The following line contains qq.
  • Each of the next qq lines contains vj,kjv_j,k_j, the parameters of query jj.

Output

Print exactly qq lines. The jj-th line is the maximum score c(vj)−m⋅kjc(v_j)-m\cdot k_j for query jj.

Subtasks

  • Subtask 1 (10 points): 1≤n,q≤121\le n,q\le12; an undirected tree has nn vertices, n−1n-1 edges and root 11; 1≤xi,yi≤n1\le x_i,y_i\le n, xi≠yix_i\ne y_i; every query satisfies 1≤vj≤n1\le v_j\le n and 0≤kj≤2⋅1050\le k_j\le2\cdot10^5.
  • Subtask 2 (20 points): 1≤n,q≤1 0001\le n,q\le1\,000; an undirected tree has nn vertices, n−1n-1 edges and root 11; 1≤xi,yi≤n1\le x_i,y_i\le n, xi≠yix_i\ne y_i; every query satisfies 1≤vj≤n1\le v_j\le n and 0≤kj≤2⋅1050\le k_j\le2\cdot10^5.
  • Subtask 3 (30 points): 1≤n,q≤30 0001\le n,q\le30\,000; an undirected tree has nn vertices, n−1n-1 edges and root 11; 1≤xi,yi≤n1\le x_i,y_i\le n, xi≠yix_i\ne y_i; every query satisfies 1≤vj≤n1\le v_j\le n and 0≤kj≤2⋅1050\le k_j\le2\cdot10^5.
  • Subtask 4 (40 points): 1≤n,q≤200 0001\le n,q\le200\,000; an undirected tree has nn vertices, n−1n-1 edges and root 11; 1≤xi,yi≤n1\le x_i,y_i\le n, xi≠yix_i\ne y_i; every query satisfies 1≤vj≤n1\le v_j\le n and 0≤kj≤2⋅1050\le k_j\le2\cdot10^5.

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 11 has child 77; vertex 77 has children 3,4,5,63,4,5,6, and vertex 33 has children 2,82,8. For (1,0)(1,0), delete 77 and 33 to give root 11 five children and score 55. For (1,2)(1,2), deleting 77 yields four children at cost 22, score 22. For (1,3)(1,3), no deletion gives score 11. For (7,1)(7,1), deleting 33 gives five children at cost 11, score 44. Vertex 55 is a leaf, so query (5,0)(5,0) yields 00. For (7,200000)(7,200000), deleting nothing leaves four direct children and score 44.

Example 2

Input

1
3
1 0
1 1
1 200000

Output

0
0
0

Explanation

There is only root vertex 11, with no children and no deletable vertices. Hence c(1)=0c(1)=0, m=0m=0, and the maximum score is zero for each of the three values of kk.