Dragster
Time limit2sMemory limit512 MB
Given pairwise win probabilities and a binary elimination bracket, compute the probability that driver 1 wins the tournament.
- Level
Medium4 of 10
- Topics
- Probability, Tree, DFS
- Solved
- No attempts yet
Problem
Dragster racing is not a popular sport in Brazil, but in the United States it draws big crowds. Fans come to watch the cars run at speeds of up to 400 km/h, even when a single run lasts only a few seconds. Many of the competitors are amateur mechanics who bolted rockets and other contraptions onto a chassis to build an extremely fast car.
A dragster competition runs as an elimination tournament. Every race puts two competitors side by side and exactly one of them is the winner, the one who finishes first. The winners are paired into new races until a single competitor is left as the champion.
Rubens is an experienced driver who raced in several categories, Formula 1 among them. After a run of setbacks he decided to devote himself to dragsters.
Because of the experience he built up in Formula 1, Rubens can watch the competitors and state, for any two of them, the probability that each one wins a race between the two.
Rubens drives well but he is weak at mathematics and at programming, so he asked you for help. You are given the probabilities Rubens computed for every pair of drivers and the description of the races in the tournament. Determine the probability that Rubens wins the tournament.
Input
The input holds several test cases.
The first line of a test case has one integer , the number of competitors in the tournament (). Competitors are identified by the integers to and races are identified by the integers to . Rubens is always competitor .
The next lines describe the probability matrix that Rubens computed. Line has real numbers separated by single spaces. Each element is the probability that competitor beats competitor . For , and ; for , . Every probability comes with exactly three decimal places.
Each of the next lines has two integers and describing one race ( and ). The first of these lines describes the race identified by , the second line describes the race identified by , and so on. and are identifiers of competitors or of races. When a race identifier appears as , the winner of race is the one who runs this race against ; when appears as , the winner of race runs against .
Each competitor and each race is used at most once as a participant of another race, so exactly one race identifier never appears as or . That race is the final and its winner is the champion.
The end of the input is a line that holds a single zero.
Output
For each test case print one line with a real number, written with exactly six decimal places, giving the probability that Rubens wins the tournament.