ACM Underground
Time limit1sMemory limit128 MB
Given metro lines, policemen on them, and two points, decide whether the destination is reachable without ever being checked, where checks happen at line changes and at non-intersection spots on lines.
- Level
Hard8 of 10
- Topics
- Geometry, Graph, BFS, Implementation
- Solved
- No attempts yet
Statement
ACM is a town with a special underground metro system built from railway segments; each segment is called a line. Every line runs trains in both directions. Two lines may intersect, and at an intersection point a passenger can switch from a train on one line to a train on the other.
The source and destination of your trip both lie somewhere on the metro lines. You board on the line that the source lies on, you may change lines only at intersection points between two lines, and you keep going until you reach the destination. Your goal is to make the whole trip free of charge, that is, without ever buying a ticket.
The obstacle is a group of policemen who check tickets, and you must never let one check yours. You can be checked in exactly two situations:
- At an intersection, but only if you actually change lines there. A policeman standing on an intersection checks you only when you switch lines at that point; if you merely pass through it while staying on the same line, he ignores you.
- While riding along a line, if you pass the exact spot of a policeman who stands on that line but not on an intersection. Such a policeman checks everyone who rides past his spot.
Policemen who are not on any metro line are irrelevant and must be ignored. You know every policeman's position in advance.

For example, in the figure above there are five metro lines and three policemen (the black circles). You can travel from to along lines without meeting any policeman, but there is no way to travel from to without being checked.
Write a program that reads the metro lines, the policemen, and the source and destination, and decides whether it is possible to travel from the source to the destination without being checked by any policeman.
Input
The first line contains an integer , the number of test cases. Each test case has the following form:
- One line with two integers and (, ): the number of metro lines and the number of policemen.
- One line with four integers : the coordinates of the source and the destination . Both points are guaranteed to lie on metro lines.
- lines, each with four integers , giving the two endpoints and of one metro line.
- lines, each with two integers , giving the position of one policeman.
All coordinates are arbitrary integers.
Output
Print lines, one per test case in the same order as the input. For each test case print a single word: YES if there is a safe way to travel from the source to the destination without being checked, or NO otherwise.