#P4618. [SDOI2018] 原题识别
[SDOI2018] 原题识别
Description
“Human problem database” Little practices very hard and has solved over ten thousand problems. Whenever someone asks him about a problem, Little can tell within second which and which problem it is. Therefore, Little is like an “original problem search engine”.
One day, Little came to a rooted tree with nodes. The root is node , and each node has a problem printed on it. With his huge amount of experience, Little quickly identified the source of every problem, and found that some problems had been reposted many times. He classified the problem on each node: the problem type of node is . The problems at node and node come from the same source if and only if .
Solving the same problem multiple times does not improve skill, except increasing the number of submissions. To investigate the quality of the problems on this tree, Little will ask the following two types of queries for a total of times:
-
: If you solve all problems on the shortest path from node to node (including and ), how many essentially different problems can you solve in total?
-
: If you choose a node uniformly at random on the shortest path from node to the root (including and the root), and choose a node uniformly at random on the shortest path from node to the root (including and the root), what is the expected answer to query ?
Let denote the number of nodes on the shortest path from node to the root. Since Little does not like fractions, and the answer to the second type of query can always be written as , you only need to output the value of .
Identifying these problems has consumed too much of Little ’s energy, so he cannot compute the answers to these simple queries by himself. Please write a program to answer all queries for Little .
Input Format
The first line contains a positive integer , denoting the number of testdata.
For each testdata, the first line contains positive integers , where is the number of nodes in the tree.
To reduce the input size to some extent, the tree edges and the problem type array will be generated by the following code:
unsigned int SA, SB, SC;
unsigned int rng61(){
SA ^= SA << 16;
SA ^= SA >> 5;
SA ^= SA << 1;
unsigned int t = SA;
SA = SB;
SB = SC;
SC ^= t ^ SA;
return SC;
}
void gen(){
scanf("%d%d%u%u%u", &n, &p, &SA, &SB, &SC);
for(int i = 2; i <= p; i++)
addedge(i - 1, i);
for(int i = p + 1; i <= n; i++)
addedge(rng61() % (i - 1) + 1, i);
for(int i = 1; i <= n; i++)
a[i] = rng61() % n + 1;
}
The second line contains a positive integer , denoting the number of queries.
The next lines each contain positive integers, in the form or , representing each query in order.
Output Format
For each testdata, output lines, one integer per line, answering each query in order.
2
5 3 10000 12345 54321
3
1 2 3
2 1 3
1 3 2
10 6 23456 77777 55555
5
1 1 10
2 3 5
2 7 5
2 5 4
1 8 6
1
5
1
4
34
61
45
3
Hint
- $1 \le T \le 3,2 \le p \le n \le 10^5,1 \le m \le 2\times 10^5$.
- $10^4 \le SA, SB, SC \le 10^6,1 \le x, y, A, B \le n$.
Subtask ( points): only contains type queries.
Subtask ( points): satisfies .
Subtask ( points): no additional constraints.
Translated by ChatGPT 5
京公网安备 11011102002149号