Dependent Events
Time limit60sMemory limit1024 MB
A rooted tree of events gives each node a conditional probability, and queries ask for the joint probability that two given nodes both occur, modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Tree, Probability, Math, DFS
- Solved
- No attempts yet
Problem
There are events, numbered through . The probability that each event occurs depends on whether exactly one other event, called its parent event, occurs. Event is the exception: it is an independent event. That is, for each event from to , three values are given: , the parent event of event ; , the probability that event occurs if its parent event occurs; and , the probability that event occurs if its parent event does not occur. For event , its probability of occurrence is given. There are queries to answer. Each query consists of two distinct events and , and you need to find the probability that both events and have occurred.
Input
The first line of the input gives the number of test cases, . test cases follow.
The first line of each test case contains two integers and , the number of events and the number of queries. lines follow. The -th line describes event . The first line contains a single integer , the probability of occurrence of event multiplied by . Each of the next lines consists of three integers , , and : the parent event of event , the probability of occurrence of event if its parent event occurs multiplied by , and the probability of occurrence of event if its parent event does not occur multiplied by . Then lines follow, describing the queries. Each of these lines contains two distinct integers and . For each query, find the probability that both events and occurred.
Output
For each test case, output one line containing Case #x: R1 R2 R3 … RQ, where is the test case number (starting from 1) and is the sought probability computed for the -th query modulo , defined precisely as follows. Represent the answer of the -th query as an irreducible fraction . The number then must satisfy the modular equation and be between and , inclusive. Under the constraints of this problem such a number always exists and is uniquely determined.
Constraints
- .
- .
- and , for all .
- , for each from to .
- , for each from to .
- .