eolymp
bolt
Try our new interface for solving problems
Problems

Traveling Salesman

Traveling Salesman

\includegraphics{https://static.e-olymp.com/content/42/42d8ca0f05688248befea56b17ecfad43957a85c.jpg} Long before the days of international trade treaties, a salesman would need to pay taxes at every border crossed. So your task is to find the minimum number of borders that need to be crossed when traveling between two countries. We model the surface of Earth as a set of polygons in three dimensions forming a closed convex 3D shape, where each polygon corresponds to one country. You are not allowed to cross at points where more than two countries meet. \textbf{Input} Each test case consists of a line containing \textbf{c}, the number of countries (\textbf{4} ≤ \textbf{c} ≤ \textbf{6000}), followed by c lines containing the integers \textbf{n x_1 y_1 z_1 ... x_n y_n z_n}, describing (in order) the \textbf{n} corners of a closed polygon (\textbf{3} ≤ \textbf{n} ≤ \textbf{20}). Then follows a line with one integer \textbf{m} (\textbf{0} < \textbf{m} ≤ \textbf{50}), and then \textbf{m} lines with queries \textbf{c_a c_b}, where \textbf{c_a} and \textbf{c_b} are country numbers (starting with \textbf{1}). No point will be on the line between two connected points, and \textbf{−10^6} ≤ \textbf{x}, \textbf{y}, \textbf{z} ≤ \textbf{10^6} for all points. No two non-adjacent edges of a country share a common point. The input is terminated by a case where \textbf{c = 0}, which should not be processed. \textbf{Output} For each query, output the number of borders you must cross to go from \textbf{c_a} to \textbf{c_b}.
Time limit 2 seconds
Memory limit 64 MiB
Input example #1
6
4 0 0 0 0 0 1 0 1 1 0 1 0
4 1 0 0 1 0 1 1 1 1 1 1 0
4 0 0 0 1 0 0 1 0 1 0 0 1
4 0 1 0 1 1 0 1 1 1 0 1 1
4 0 0 0 0 1 0 1 1 0 1 0 0
4 0 0 1 0 1 1 1 1 1 1 0 1
2
1 2
1 3
0
Output example #1
2
1
Source 2006 Nordic Collegiate Programming Contest, September 30, Problem F