P18154 [NWERC 2026] Casting Spells
Description
Every year, on the day of the summer solstice, the magic academy holds its annual spellcasting competition.
There are $s$ different spells taught at the academy, some of which are considered more difficult than others. The $n$ participating students are numbered from $1$ to $n$. For every spell and every participant, the participant either knows the spell or they do not.
On the day of the competition, at sunrise, the chancellor of the academy goes through the list of spells. For every spell, she flips a fair coin to decide whether the spell is included in this year's competition or not. It is possible that all spells are included in the competition, or that none are.
The competition consists of a number of one-on-one duels between participants, with every participant facing every other participant exactly once. A duel works as follows: the participants go through those spells that were selected for the competition, in order from most difficult to least difficult. If one of them knows the spell and the other does not, then the participant who knows the spell wins the duel and the duel ends. If they both know the spell or they both do not, then they move on to the next spell in the competition. If they reach the end of the list of spells included in the competition, then the duel is considered a draw.
In the end, the participants are ranked from $1$ to $n$ by the number of duels they won, where the participant with rank $1$ has won the most duels. Draws do not count towards the number of duels won. If two participants have won the same number of duels, the one with the smaller participant number will have a smaller rank.
It is the day before the competition. You are a bookmaker taking bets on the participants' performances. To ensure that you turn a profit, you would like to determine, for each participant, their expected position in the final ranking.
Input Format
The input consists of:
- One line with two integers $n$ and $s$ ($1 \le n \le 150\,000$, $1 \le s \le 18$), the number of students participating in the competition and the number of spells taught at the academy.
- One line with $s$ strings, the names of the spells taught at the academy, ordered from most difficult to least difficult. The $s$ spell names are pairwise distinct. Each name has a length between $1$ and $20$ characters and consists of an uppercase English letter followed by zero or more lowercase English letters.
- $n$ lines, the $i$th of which contains an integer $s_i$ ($0 \le s_i \le s$), followed by $s_i$ strings, the names of the spells known by participant $i$. The spells are pairwise distinct and taught at the academy.
Moreover, for each participant, consider the total length of the names of the spells they know. The sum of these values over all participants is at most $3 \cdot 10^6$.
Output Format
For each participant, in order from $1$ to $n$, output their expected position in the final ranking.
Each of these should have an absolute or relative error of at most $10^{-6}$.