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.
Hard8GraphBFSImplementationNo attempts yetTime limit10sMemory limit512 MBThe 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 t 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.
The first line contains one integer t (1≤t≤200000), the number of tables.
Each of the next t lines holds the list handed in by one counter. The line starts with a letter p, the type of the list, and an integer k (0≤k≤t−1), the length of the list. Then come k distinct integers a1,…,ak (1≤ai≤t). The letter p is either C or N.
If p is C, the scrutineer at this table can watch the tables a1,…,ak and no other table. If p is N, the scrutineer at this table can watch every table except a1,…,ak. 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 t in this order. If the scrutineer at table x can watch table y, then the scrutineer at table y can watch table x. The lengths of all lists add up to at most 500000.
If no fair assignment exists, print Impossible.
Otherwise print one string of length t made of the letters S, for a Sprite scrutineer, and V, for a Virus scrutineer. The i-th letter is the scrutineer at table i.
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 r be the smallest table number, and let di be the smallest number of neighbour steps that lead from r to table i, so that dr=0. The i-th letter is S when di is even, and V when di is odd.