#P9898. Inference
Inference
Inference
Problem Description
Given features, you want to infer the last feature's value of Alice by the value of her first features, based on massive data. The relationship between features can be represented as a directed acyclic graph, where a node pointing to a node indicates that is a random variable dependent on . The following formula can calculate the probability of the last feature's value of Alice based on other data, in which represents the th feature, represents nodes that point to , and represents their number of occurrences:
$$P(x_m|x_1,\dots,x_{m-1})=cP(x_1,\dots,x_m)=\prod_{i=1}^m P(x_i|\pi(x_i)) $$$$P(x_i|\pi(x_i))=\frac{P(\pi(x_i), x_i)}{P(\pi(x_i))}=\begin{cases}\frac{cnt(\pi(x_i), x_i)}{cnt(\pi(x_i))},\text{ if }cnt(\pi(x_i))\ne 0\\\\0,\text{ if }cnt(\pi(x_i))=0\end{cases} $$It is guaranteed that the last feature has zero out-degree, and there exists at least one case of value such that . Now, given people's data, what is the most likely value of the th feature of Alice, given the value of her first features.
Input
At the beginning of the input section, there is an integer , indicating the number of test cases. For every test case, the first line consists of three integers , , , indicating the number of data, the number of features, and the number of the relationship between the features. In the next rows, each row contains two integers , indicating node point to node , thus th feature is dependent on th feature. Every point has an in-degree less than 5. In the next rows, each row contains integers indicating one person's values of each feature. Every feature's value is among . In the last line, there are integers indicating Alice's values of the first features. (请不要使用 scanf 进行读入)
Output
An integer denoting the most probable value of Alice's th feature. It is guaranteed that there exists only value which has the biggest probability.
Sample Input
1
10 5 4
1 4
2 4
3 5
4 5
0 1 0 1 0
0 1 1 1 1
0 0 0 0 0
0 0 1 0 0
1 1 1 0 1
1 0 0 1 1
1 1 0 1 1
1 0 1 0 0
1 1 1 1 1
0 1 0 1 1
1 1 0 0
Sample Output
0
Hint
Source
2023“钉耙编程”中国大学生算法设计超级联赛(9)