Tournament turn-around
InterviewTime limit5sMemory limit512 MB
Given head-to-head win counts for eight teams, find the bracket order that maximizes team 1's chance of winning the tournament.
- Level
Easy3 of 10
- Topics
- Brute force, Probability
- Solved
- No attempts yet
Problem
In the region of Concordia, the teams of a famous alliance of technical universities hold a wallyball tournament every year. Your university, Beaver Valley Institute of Mines and Telecommunication Tech, is a proud participating institution. The model of the tournament is quite simple. There are eight teams, and they are arranged in four pairs to play a quarter-final. The winners of the quarter-final are arranged in two pairs to play two semi-finals. Finally, the winners of the semi-finals meet in the final.

This year is a bit special because the alliance between the universities is about to break down. Your alma mater will lose part of its name, and this is your university's last chance to win the trophy that no one will ever be able to claim back. Your university has always practiced fair play and would never cheat. But what if good fortune could help your team a little by drawing a favorable order for the teams?
The Bureau des Sports has collected extensive statistics. Over the alliance's short history, you know how many matches each team has won over the others. We assume this gives the probability of the outcome of each individual match. If team has won times over team , and team has won times over team , then in a match between and , team wins with probability and team wins with probability . There are no ties. If historically has never won against , then has no chance of winning at the tournament. You have seen every pair of teams play at least once, so the probabilities are always well-defined.
According to this probabilistic model, what order of teams maximizes the probability that your team wins the entire tournament?
Input
The input file consists of multiple test cases. The first line contains a single integer , the number of test cases. Each test case consists of eight lines of eight integers separated by single spaces. For , the th integer in the th line is how many matches team won against team . Each integer is between zero and five. The integers on the diagonal are always zero. Every pair of teams has played at least once, so for , either the th integer of the th line or the th integer of the th line is nonzero. Your university is always the first university in the input ordering.
Output
For each test case, print one line with a float followed by eight integers separated by a single space. The float is the maximum probability that your university wins the tournament, rounded to exactly 5 decimal places. The eight integers give the order of teams that achieves this probability, assuming the matches proceed according to the diagram above and each match is decided by the probabilistic model. If multiple arrangements give the same probability, print the lexicographically smallest one.