Polygons
Time limit1sMemory limit128 MB
A convex polygon is triangulated, one triangle is black; players alternately cut off an ear triangle, and whoever removes the black triangle wins. Decide if the first player wins.
- Level
Hard8 of 10
- Topics
- Game theory, Tree, DFS, Greedy
- Solved
- No attempts yet
Problem
Two players play the game of polygons. You are given a convex polygon with vertices that has been divided by pairwise non-crossing diagonals into triangles. The diagonals meet only at vertices of the polygon. One of the triangles is black and all of the others are white.
The players move in alternating turns. On a turn, the current player cuts exactly one triangle away from the current polygon along a diagonal. The only triangle that may be cut off is one that has a diagonal as one of its sides and two sides of the current polygon as its other two sides; cutting it removes that triangle from the polygon. The player who cuts away the black triangle wins.
A polygon is convex if the segment joining any two of its points lies entirely inside the polygon.
Write a program that reads the description of the polygon and decides whether the player who moves first has a winning strategy.
Input
The first line contains an integer , the number of vertices of the polygon (). The vertices are numbered clockwise from to .
Each of the next lines describes one triangle. The -th of these lines () contains three non-negative integers , , separated by single spaces, the numbers of the vertices of the -th triangle. The triangle given first is the black one.
Output
Print a single line containing TAK if the player who moves first has a winning strategy, or NIE otherwise. (TAK and NIE mean 'yes' and 'no' in Polish.)