This page is still under construction.

Parts of this page are still being built. What you see may change.

Gambling Machine

Time limit1sMemory limit128 MB

Summary
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 nn integer generators G1,G2,…,GnG_1, G_2, \ldots, G_n, where 1≤n≤10001 \le n \le 1000. Each generator GiG_i is tied to a fixed set Si⊆{1,2,…,n}S_i \subseteq \{1, 2, \ldots, n\}. Let ki=∣Si∣k_i = |S_i|; a set may be empty, and the sum k1+k2+⋯+knk_1 + k_2 + \cdots + k_n never exceeds 1200012000.

Each time a generator is activated it produces one integer, according to these rules:

  • The first time GiG_i is activated it produces some element of SiS_i.
  • On every later activation GiG_i produces an element of SiS_i that it has not produced before. You may choose, for each generator, the order in which its elements come out.
  • Once every element of SiS_i has already been produced (in particular when SiS_i is empty), GiG_i produces 00.

The machine always starts by activating G1G_1. After a generator produces a positive integer rr, the next generator activated is GrG_r. As soon as some generator produces 00, the machine halts.

The machine is defeated when the halting 00 is produced by the last generator GnG_n and, at that moment, every generator has already used up its whole set (every element of every SiS_i 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 00 without being a defeat.

Decide whether the given machine is well constructed.

Input

The first line contains one integer nn (1≤n≤10001 \le n \le 1000), the number of generators. Each of the next nn lines describes one generator: line i+1i + 1 contains kik_i followed by the kik_i elements of SiS_i, given in arbitrary order and separated by single spaces. Every element lies in {1,…,n}\{1, \ldots, n\}, the elements on one line are distinct, and k1+⋯+kn≤12000k_1 + \cdots + k_n \le 12000.

Output

Print TAK if the machine is well constructed, and NIE otherwise.

Examples3

  1. Example 1

    Input
    2
    2 1 2
    1 2
    
    Expected output
    TAK
    
  2. Example 2

    Input
    2
    1 2
    0
    
    Expected output
    NIE
    
  3. Example 3

    Input
    3
    2 3 2
    2 1 3
    1 2
    
    Expected output
    TAK