#P1235. 血缘关系
血缘关系
Description
We are studying the blood relationships within a family of yaoguai (妖怪, yaoguai). Each yaoguai has the same number of genes, but the genes of different yaoguai may differ. We want to know how many genes are the same between any two given yaoguai. Since the number of genes is massive, direct testing is not feasible. However, we know the family tree, so we can estimate the number of shared genes between two yaoguai based on the family tree.
The inheritance of genes among yaoguai is quite simple: if yaoguai is the child of yaoguai and , then any single gene of can only be inherited from either or , with a probability for each. All genes are considered independent, and the inheritance of any gene is not affected by other genes.
Now, we define the gene similarity between two yaoguai and . For example, consider a family with two unrelated yaoguai and (no shared genes), and their children and . What is the similarity between and ? Since the genes of and both come from and , each with a probability of , in expectation and share of their genes, so the gene similarity between and is . Note that if and have shared genes, then the similarity between and will no longer be .
Your task is to write a program that, given the family tree and several pairs of yaoguai, computes their gene similarity.
Input Format
The first line contains two integers and . Here is the number of members in the family, labeled . is the number of yaoguai in this family who have parents (the remaining yaoguai have no parents given; they can be regarded as unrelated to each other, i.e., sharing no genes).
The next lines each contain three integers , meaning yaoguai is the child of yaoguai and .
Then a line with one integer (), the number of yaoguai pairs for which the gene similarity is to be computed.
The next lines each contain two integers, representing a pair of yaoguai whose gene similarity should be computed.
You may assume the given family tree is always valid. Specifically, no yaoguai will be an ancestor of themself, and you do not need to worry about gender inconsistencies.
Output Format
Output lines. The -th line corresponds to the gene similarity of the -th pair of yaoguai. You must output it as a percentage, with as much precision as there actually is, and it must be exact, but do not print extra trailing s. Also, a leading zero before the decimal point is required (note that for you should output , not ). See the sample for the exact format.
7 4
4 1 2
5 2 3
6 4 5
7 5 6
4
1 2
2 6
7 5
3 3
0%
50%
81.25%
100%
Hint
Translated by ChatGPT 5
京公网安备 11011102002149号