SP6804 CHEATING - Cheating or Not
Description
For the organizers of a soccer world championship the final draw is a very delicate job. It determines the compositions of the groups for the first stage of the tournament and indirectly also the possible matches in the knockout stage. The importance lies in the fact that the success of a team might depend on the opponents it faces - and, maybe, even the winner of the tournament.
The final draw is often subject to accusations of fraud. Some teams tend to think that their group is stronger than others and therefore complain they were cheated. Your job is to provide some facts that can help convince them of the opposite.
The draw is somewhat complicated due to a number of fairness considerations. The objective is not to assign too many good teams to the same group. Also teams from the same confederation should be drawn into different groups. This is ensured by the following rules.
- There are _g_ groups with _m_ members each.
- The hosting nation will be seeded first in the first group.
- _g-1_ selected teams will be seeded first in the remaining groups.
- The remaining positions are drawn from _m-1_ pots, one team from each pot per group.
- You will be told which teams belong to the same confederation and you have to ensure that no two teams of the same confederation are in the same group. For confederations with more than _g_ teams this is impossible, so for these confederations you can ignore this rule.
- You may assume that for confederations with at most _g_ teams, all teams of the confederation which are not seeded are in the same pot.
- Note that each team belongs to exactly one confederation and each team is either seeded or contained in exactly one pot.
We want to compute the average strength of the opponents of a given team. The strengths of the teams will be given in the input. Now you have to compute the average of the sum of the strengths of the other teams in the group of the given team. The average is evaluated over all correct draws which are assumed to have the same likelihood.
Input Format
The input starts with the number of test cases. Each test case is described as follows.
The first line contains the number of groups _g and the number of teams per group _m . A line with _g \* m_ integers follows. The _i_-th integer _0 denotes the strength of the _i_-th team.___
The team indices start from 0. By convention, the hosting nation is assigned number 0. The next line lists the _g-1_ seeded teams by their numbers. Each of the _m-1_ following lines contains _g_ teams which are allocated to the same pot.
The next line specifies the number of confederations _c_. _c_ lines follow which describe one confederation each. Each confederation description starts with the number of teams _n $ _{i} $ > 0_. Then _n $ _{i} $_ numbers with the team indices follow.
The last line contains the number _t_ of the team, whose average group strength has to be evaluated.
Output Format
Output the average of the sum of strengths of the opponents of team _t_ in the group stage with 3 decimals on a single line.