problems / 170

The Qualifying Bracket (medium version)

medium probability dp games tournament 2000 ms · 256 MB

The Qualifying Bracket (medium version)

Suki (dog 1) is competing in a single-elimination dog show tournament. There are n dogs total, each with a fixed starting position in the bracket (dog i at position i). The bracket is a complete binary tree: in the first round, positions 1 vs 2, 3 vs 4, ..., (n-1) vs n compete; winners advance to the next round, and so on until one dog remains.

For every pair of distinct dogs i and j, we know the probability p[i][j] that dog i beats dog j in a head-to-head match. We want to compute the probability that Suki (dog 1) wins the entire tournament.

Input

The first line contains n (2 ≤ n ≤ 512, and n is a power of 2), the number of dogs.

The next n·(n−1)/2 lines each contain three space-separated values: integers i and j (1 ≤ i, j ≤ n, i < j) and a decimal number p (0 ≤ p ≤ 1), meaning dog i beats dog j with probability p. The probability that dog j beats dog i is therefore 1 − p.

All pairs (i, j) with i < j appear exactly once.

Output

Print a single decimal number: the probability that dog 1 wins the tournament. Your answer must be within 10^−6 of the correct value (absolute or relative error).

Notes

The bracket structure is fixed and determined solely by the dog positions. In the first round, dogs at positions 1 and 2 face each other; dogs at positions 3 and 4 face each other; and so on. Winners of each match advance to face the winners of the adjacent match at the next level, up the bracket tree.

Answers within 1e-06 absolute or relative error are accepted.

Example 1

Input

2
1 2 0.6

Output

0.6

Example 2

Input

4
1 2 0.5
1 3 0.5
1 4 0.5
2 3 0.5
2 4 0.5
3 4 0.5

Output

0.25

Hints · reveal one at a time if you're stuck

Hint 1

Suki wins only if she wins every match on her path to the final. In each round, who can her opponent be, and how is that set of dogs determined by the bracket?

Hint 2

The probability that a dog wins a sub-bracket can be built from the two halves below it. Notice that Suki's result depends only on the winner probabilities of the sibling sub-brackets she must face.

Hint 3

Compute the winner probabilities of each sub-bracket bottom-up, combining the two halves with the head-to-head probabilities. Then multiply Suki's probability of beating each sibling block's winner, summed over that block, across the rounds.

report an issue Prefer your own editor? sooks prep 170 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.