Gambling Machine
Time limit1sMemory limit128 MB
Given n generators, each mapping to a subset of generators, decide whether some choice of output order lets the machine halt at Gn with all sets exhausted (defeat) or must it halt elsewhere.
- Level
Medium7 of 10
- Topics
- Graph, DFS, Greedy, Implementation
- Solved
- No attempts yet
Problem
A gambling machine is built from integer generators , where . Each generator is tied to a fixed set . Let ; a set may be empty, and the sum never exceeds .
Each time a generator is activated it produces one integer, according to these rules:
- The first time is activated it produces some element of .
- On every later activation produces an element of that it has not produced before. You may choose, for each generator, the order in which its elements come out.
- Once every element of has already been produced (in particular when is empty), produces .
The machine always starts by activating . After a generator produces a positive integer , the next generator activated is . As soon as some generator produces , the machine halts.
The machine is defeated when the halting is produced by the last generator and, at that moment, every generator has already used up its whole set (every element of every has been produced). The machine is well constructed when there exists at least one run, over all the ordering choices above, that halts on a without being a defeat.
Decide whether the given machine is well constructed.
Input
The first line contains one integer (), the number of generators. Each of the next lines describes one generator: line contains followed by the elements of , given in arbitrary order and separated by single spaces. Every element lies in , the elements on one line are distinct, and .
Output
Print TAK if the machine is well constructed, and NIE otherwise.