This page is still under construction.

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

Bytehattan

Time limit10sMemory limit128 MB

Summary
After each street closure in an n by n grid, decide whether the two endpoints of the closed street stay connected by open streets.
Level

Medium7 of 10

Topics
Union-find, Graph
Solved
No attempts yet

Problem

Bytehattan is one of the islands in the capital of Byteland. Parades, outings and processions are held there so often that streets close and traffic jams up badly. Byteasar, who works at the town hall, has been put in charge of watching the island's traffic.

The streets of Bytehattan form a regular n×nn \times n grid. Read the map as grid coordinates. For every pair of integers x,yx, y with 1≤x,y≤n1 \le x, y \le n there is an intersection at the point (x,y)(x, y), and every two intersections at distance 1 are joined by a street of length 1.

Messages about street closures keep arriving. One message means that one street closes from now on. Once Byteasar learns that a street has closed, he has to decide whether the two intersections at its ends can still be reached from each other along streets that are not closed yet. Write a program that helps him.

Input

The first line contains two integers nn and kk (2≤n≤15002 \le n \le 1500, 1≤k≤2n(n−1)1 \le k \le 2n(n-1)). Here nn is the number of intersections along one side of the grid and kk is the number of closure messages. Each of the next kk lines gives the closure information for one street, in chronological order. Each of those lines lists two streets one after the other, but exactly one of them actually closes. If the two intersections at the ends of the street closed in the previous line could still be reached from each other, the first of the two streets closes. If they could not, the second one closes. The first of the kk closures applies to the first of the two streets on its line. No street closes twice.

A single street is written as an integer pair aia_i, bib_i (1≤ai,bi≤n1 \le a_i, b_i \le n) followed by a letter cic_i (ci∈{N,E}c_i \in \{N, E\}). One end of that street is the intersection at (ai,bi)(a_i, b_i). If ci=Nc_i = N, the other end is the intersection at (ai,bi+1)(a_i, b_i + 1). If ci=Ec_i = E, the other end is the intersection at (ai+1,bi)(a_i + 1, b_i). If ci=Nc_i = N then bi<nb_i < n, and if ci=Ec_i = E then ai<na_i < n.

The jury picked this unusual input format on purpose, to force each closure to be processed before the next one is read.

Output

Print exactly kk lines. If the two intersections at the ends of the street closed by the ii-th message can still be reached from each other afterwards, print TAK (Polish for yes) on the ii-th line. Otherwise print NIE (Polish for no) on the ii-th line.

Examples1

  1. Example 1

    Input
    3 4
    2 1 E 1 2 N
    2 1 N 1 1 N
    3 1 N 2 1 N
    2 2 N 1 1 N
    
    Expected output
    TAK
    TAK
    NIE
    NIE