Sunlight on a Tree
Time limit5sMemory limit256 MB
Report all nodes on the tree path from u to v whose dot product with the query direction is minimal.
- Level
Hard8 of 10
- Topics
- Tree, Segment tree, Geometry
- Solved
- No attempts yet
Problem
Look down on a tree from directly above. Each branch is an edge and each point where branches meet or end is a node, so the top view is a graph drawn on the plane. Edges of the drawing may cross, but the graph itself is a tree: it is connected and has no cycle, and its nodes, each placed at a 2D coordinate, are joined by edges.
A query gives four numbers , , , . Here and are two nodes, and the vector is the direction the sunlight travels. The sunlight is not a single ray. It is a bundle of parallel rays that comes from infinitely far away and moves along . The figure below draws : the light arrives from the lower left and every ray is parallel to .

A node is warmer when the light reaches it earlier. The rays travel along , so the light reaches a node at earlier when is smaller. An ant walks from to along the unique shortest path of the tree. Report every node on that path that the sunlight reaches first, that is, every node of the path whose value of is minimum.

The second figure shows a tree with 14 nodes. For , , , the path is 14, 11, 1, 3, 4, the light comes from the left, and the sunlight reaches nodes 3 and 11 first. For , , , the path is 13, 11, 1, 6, 9, the light comes from the upper left, and the sunlight reaches nodes 1, 6 and 11 first.
Input
The first line contains the number of test cases (). Each test case starts with a line holding the number of nodes (). The next lines hold the node coordinates, one node per line, and the -th of those lines gives the coordinates of node (). Several nodes may sit at the same coordinates. Each of the next lines holds the ids of the two nodes that an edge connects, and an id is between 1 and . The input is always a valid tree. The next line holds the number of queries (). Each of the next lines holds one query in the form (, ). The vector is never the zero vector, and and may be the same node. Not every test case is a worst case, but at least one of them uses the largest number of nodes and queries.
Output
For each test case print a line Case k:, where is the number of the test case counting from 1. Then print one line per query holding the ids of the nodes that the sunlight reaches first, sorted in increasing order and separated by single spaces. Do not put a space at the beginning or the end of a line. Over the whole input your program prints at most ids.