Election Frenzy
Time limit10sMemory limit512 MB
Given each table's visible set either explicitly or as a complement, build the visibility graph and 2-color each connected component by BFS distance parity.
- Level
Hard8 of 10
- Topics
- Graph, BFS, Implementation
- Solved
- No attempts yet
Problem
The campaign is over. Phong of the Sprites Party and Megabyte of the Viruses Party are waiting for the result, and all that is left is counting the votes.
The votes are counted in a room with tables. One counter sits at each table. Both parties may send people into the room to watch that the counting is done fairly, and such a person is called a scrutineer. One scrutineer from each party at every table would be ideal, but the room has space for only one scrutineer per table.
You have to assign the scrutineers. Every table gets exactly one scrutineer, either a Sprite or a Virus. Tables that stand close together can be watched together: a scrutineer watches the counting at their own table and at every table visible from it. An assignment is fair if every table is watched by at least one Sprite and by at least one Virus.
Each counter wrote down a list of table numbers. Some counters wrote the tables that are visible from their own table, and the rest wrote the tables that are not visible from it.
Find a fair assignment of scrutineers.
Input
The first line contains one integer (), the number of tables.
Each of the next lines holds the list handed in by one counter. The line starts with a letter , the type of the list, and an integer (), the length of the list. Then come distinct integers (). The letter is either C or N.
If is C, the scrutineer at this table can watch the tables and no other table. If is N, the scrutineer at this table can watch every table except . A list never holds the number of its own table, because a scrutineer always watches the table they sit at.
The lines describe table 1 to table in this order. If the scrutineer at table can watch table , then the scrutineer at table can watch table . The lengths of all lists add up to at most .
Output
If no fair assignment exists, print Impossible.
Otherwise print one string of length made of the letters S, for a Sprite scrutineer, and V, for a Virus scrutineer. The -th letter is the scrutineer at table .
Several fair assignments can exist, so print this one. Call two tables neighbours when each can watch the other, and group the tables that are joined by chains of neighbours. In every group let be the smallest table number, and let be the smallest number of neighbour steps that lead from to table , so that . The -th letter is S when is even, and V when is odd.