Given two planar connected networks, find the Red-Blue junction pair allowed to be joined by a segment, following a fixed angular tie-breaking rule.
Hard9GeometrySortingGraphBinary searchNo attempts yetTime limit0.4sMemory limit32 MBByteland has two highway networks, one run by Red and one run by Blue. Each network consists of junction points and straight segments that join pairs of junction points. Inside one network two segments meet only at junction points and nowhere else. Each network is connected, so you can travel between any two junction points of the same network along its segments. No junction point belongs to both networks. Segments of different networks may cross each other.
The two companies are merging. They want to join the networks by building one straight segment between a junction point of Red and a junction point of Blue. Apart from its own two endpoints, the new segment must share no point with an existing segment.
The input holds the description of the Red network followed by the description of the Blue network. The first line of a description has two integers N and M (2≤N≤200000, 1≤M≤700000), the number of junction points and the number of segments. Each of the next N lines has two integers x and y (−1000000≤x,y≤1000000), the coordinates of a junction point. Junction points are numbered from 1 to N in the order they appear. Each of the next M lines has two integers p and q (1≤p,q≤N, p=q), the endpoints of a segment.
No junction point lies on a segment of the other network.
Print two integers u and v, the endpoints of the new segment. u is a junction point of Red, v is a junction point of Blue, and the segment between them meets no segment of either network except at u and v.
Several pairs can satisfy that, so the answer is fixed by the rule below.
Call a point P higher than a point Q if P has the larger y, or the same y and the larger x. Let t be the highest of all junction points of both networks, and let b be the highest junction point of the network that does not contain t. Consider only those junction points a of the network of t that are higher than b and can be joined to b under the condition above. Such an a always exists. Among them take the one whose vector from b to a makes the smallest counterclockwise angle with the positive x direction. That angle is always at least 0 and less than 180 degrees. If two points give the same angle, take the one closer to b. For the pair chosen this way, print the number of the Red junction point first and the number of the Blue junction point second.
The figure shows the two networks of the example, with the connecting segment that the rule selects drawn as a dashed line.
