Signatures
Time limit3sMemory limit512 MB
Given vouching edges among workers, commanders have no guarantors; list clerks whose reachability from some non-spy commander depends on a single commander.
- Level
Medium7 of 10
- Topics
- Graph, DFS, Greedy, Implementation
- Solved
- No attempts yet
Problem
The Byteland Security Agency (BSA) employs two kinds of workers: commanders and clerks. Every worker is identified by a number. For each clerk, the archive keeps the clerk's own signature together with the signatures of the workers (clerks or commanders) who vouch for that clerk's loyalty. Every clerk must be vouched for by at least one worker, and over time more guarantors may be added. A commander is a worker whom nobody vouches for.
BSA has discovered that a spy from hostile Microsoftland has slipped into the ranks of the commanders. Afterwards, more spies were hired into clerk positions, each vouched for only by workers who are themselves spies (the spy-commander and/or other spies already hired). In other words, the guarantors of a spy clerk are exclusively spies.
A clerk's reliability is questioned when the clerk has not obtained a guarantee, directly or indirectly, from any commander who is not a spy. Formally, the clerk is questioned when there is no sequence of workers such that is a commander who is not a spy, is that clerk, and vouches for for every .
A clerk is suspected of spying if assuming some one commander to be a spy causes that clerk's reliability, which was not in question beforehand, to become questioned. The BSA command wants the complete list of such clerks.
Write a program that reads the number of workers and the guarantee information, determines the clerks suspected of spying, and writes them to standard output.
Input
The first line contains one integer (), the number of BSA workers. The workers are numbered from to .
Each of the next lines describes the guarantees for one worker: line (for ) describes worker . The line begins with an integer (), the number of guarantees worker has received, followed by integers giving the numbers of the workers who vouch for worker . All numbers on a line are separated by single spaces, so the line holds integers in total.
A worker whom nobody vouches for (that is, ) is a commander; every other worker is a clerk.
Output
If at least one clerk is suspected of spying, print their numbers in increasing order, one per line.
If no clerk is suspected, print a single line containing the word BRAK.
Hint
Suppose the commanders are Alice and Gregor, and the clerks are vouched for as follows:
- Bob is vouched for by Alice.
- Charlie is vouched for by Bob.
- David is vouched for by Bob and by Charlie.
- Eve is vouched for by Alice and Gregor.
- Frank is vouched for by Eve.
- Henry is vouched for by Gregor and Isabelle.
- Isabelle is vouched for by Gregor and Henry.
Bob, Charlie, and David can be reached only from Alice, so assuming Alice is a spy makes them questioned. Henry and Isabelle can be reached only from Gregor, so assuming Gregor is a spy makes them questioned. Eve and Frank can be reached from both commanders, so no single spy assumption questions them. The clerks suspected of spying are therefore Bob, Charlie, David, Henry, and Isabelle.