Bytehattan
Time limit10sMemory limit128 MB
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 grid. Read the map as grid coordinates. For every pair of integers with there is an intersection at the point , 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 and (, ). Here is the number of intersections along one side of the grid and is the number of closure messages. Each of the next 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 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 , () followed by a letter (). One end of that street is the intersection at . If , the other end is the intersection at . If , the other end is the intersection at . If then , and if then .
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 lines. If the two intersections at the ends of the street closed by the -th message can still be reached from each other afterwards, print TAK (Polish for yes) on the -th line. Otherwise print NIE (Polish for no) on the -th line.