Points
Time limit1sMemory limit128 MB
Given up to 10000 directional rules between n points, decide whether real coordinates satisfy all of them at once.
- Level
Medium7 of 10
- Topics
- Graph, DFS, Implementation
- Solved
- No attempts yet
Problem
There are points on the plane. Let the coordinates of point be . You are given rules of the form , each stating that the relation holds between the positions of points and . For example, " NE " indicates that point lies to the NorthEast of point .
The relation is one of eight kinds , corresponding to the eight directions on the plane. Depending on the value of , means exactly one of the following:
- N (North): and
- E (East): and
- S (South): and
- W (West): and
- NE (NorthEast): and
- NW (NorthWest): and
- SE (SouthEast): and
- SW (SouthWest): and
Determine whether it is possible to place the points on the plane so that all given rules are satisfied.
Input
The first line contains a single integer (), the number of test cases. The first line of each test case contains two integers (), the number of points, and (), the number of rules. Each of the following lines contains one rule of the form , meaning that point has relation with point .
Output
For each test case, print a single line containing either POSSIBLE or IMPOSSIBLE, indicating whether the points can be placed on the plane according to the given rules.