Spacecraft Malfunction
InterviewTime limit1sMemory limit128 MB
Given each unit's check reports on two others, exactly one faulty unit, and a faulty unit may lie, find the unique faulty unit or print impossible.
- Level
Medium5 of 10
- Topics
- Brute force, Implementation, Graph, Simulation
- Solved
- No attempts yet
Problem
It is said that court intrigues began with people lying about others, and then lying about those lies, and so it went. The intriguers were forever looking for a scapegoat, who inevitably turned out to be whoever had the least power — though not always the least morality.
We have run into a similar problem, except this time it is aboard our malfunctioning spacecraft! The spacecraft contains a number of units. The units are so reliable that it would greatly surprise us if more than one were faulty. If more than one were faulty we would lose the probe, so we are certain that exactly one unit is faulty.
Each unit checks exactly two other units, and each unit is checked by at least one other unit. A good unit gives an accurate diagnosis of the units it checks. For example, if unit is good and it says that is faulty and is good, then in fact is faulty and is good. A faulty unit, however, is unreliable: if unit is faulty and makes the same statements, then may or may not be good, and may or may not be good either. A unit cannot check itself.
Given the reports from all units, your task is to determine which unit is actually faulty.
Input
The first line contains a single integer (), the number of test cases, followed by the data for each test case.
The first line of each test case contains an integer (), the number of units, followed by lines, each describing one unit and the results of its checks. Every line starts with a positive integer, the identification number of the unit. After the id number come two pairs of (checked unit id, check result). A check result is a single character, either Y or N, indicating whether the checked unit was reported good or faulty, respectively.
For example, a line 16 8 Y 32 N means that unit checked unit and reported it good (Y), and checked unit and reported it faulty (N).
Output
Print one line per test case. If the faulty unit can be uniquely determined, print its identification number; otherwise — when no candidate is consistent, or more than one is — print the lower-case word impossible.