#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 CC is the child of yaoguai AA and BB, then any single gene of CC can only be inherited from either AA or BB, with a 50%50\% 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 XX and YY. For example, consider a family with two unrelated yaoguai AA and BB (no shared genes), and their children CC and DD. What is the similarity between CC and DD? Since the genes of CC and DD both come from AA and BB, each with a probability of 50%50\%, in expectation CC and DD share 50%50\% of their genes, so the gene similarity between CC and DD is 50%50\%. Note that if AA and BB have shared genes, then the similarity between CC and DD will no longer be 50%50\%.

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 n (2n300)n\ (2 \le n \le 300) and kk. Here nn is the number of members in the family, labeled 1,2,,n1,2,\cdots,n. k (0kn2)k\ (0 \le k \le n-2) 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 kk lines each contain three integers a,b,ca, b, c, meaning yaoguai aa is the child of yaoguai bb and cc.

Then a line with one integer mm (1mn21 \le m \le n^2), the number of yaoguai pairs for which the gene similarity is to be computed.

The next mm 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 mm lines. The ii-th line corresponds to the gene similarity of the ii-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 00s. Also, a leading zero before the decimal point is required (note that for 0.0010.001 you should output 0.1%\verb!0.1%!, not .1%\verb!.1%!). 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