Twin Trees Bros.
Time limit3sMemory limit512 MB
Given two 3D trees, count the node correspondences under which one tree maps onto the other by translation, uniform positive scaling, and axis rotations.
- Level
Hard8 of 10
- Topics
- Tree, Geometry, Backtracking
- Solved
- No attempts yet
Problem
To meet the demand of ICPC (International Cacao Plantation Consortium), you have to check whether two given trees are twins.

Example of two trees in the three-dimensional space.
In graph theory, a tree is a connected graph whose number of edges is one less than its number of nodes. ICPC also gives each tree node as a point on a three-dimensional grid. Two trees are twins if there is a geometric transformation function that maps the nodes of one tree one-to-one onto the nodes of the other, such that for each edge of one tree, there is an edge in the other tree connecting the corresponding nodes. The transformation must be a combination of the following:
- translations, which add constants to the coordinate values,
- uniform scaling with positive scale factors, which multiply all three coordinate values by the same positive constant, and
- rotations by any amount around the x-, y-, or z-axis.
Two trees can be twins in more than one way, with different node correspondences.
Write a program that decides whether two trees are twins and outputs the number of different node correspondences.
Transformations are described in the right-handed xyz-coordinate system.
The trees in sample inputs 1 through 4 are shown in the following figures. The numbers in the figures are the node numbers defined below.

For sample input 1, each node of the red tree maps to the corresponding node of the blue tree by a transformation that translates by (-3, 0, 0), rotates by around the z-axis, rotates by around the x-axis, and finally scales by . Under this mapping, nodes 1, 2, and 3 of the red tree, at (0, 0, 0), (1, 0, 0), and (3, 0, 0), correspond to nodes 6, 5, and 4 of the blue tree, at (0, 3, 3), (0, 2, 2), and (0, 0, 0). This is the only possible correspondence of the twin trees.
For sample input 2, red nodes 1, 2, 3, and 4 can map to blue nodes 6, 5, 7, and 8. Another correspondence maps nodes 1, 2, 3, and 4 to 6, 5, 8, and 7.
For sample input 3, the two trees are not twins. Some transformations map the nodes of one tree to distinct nodes of the other, but the edge connections do not agree.
For sample input 4, no transformation maps the nodes of one tree to those of the other.
Input
The first line contains , the number of nodes in each tree, where . Descriptions of two trees follow. A description consists of lines giving the vertex positions and lines giving the edges. The first tree has nodes numbered 1 through , and the second tree has nodes numbered through .
The input has the following format.
n
x1 y1 z1
.
.
.
xn yn zn
u1 v1
.
.
.
un−1 vn−1
xn+1 yn+1 zn+1
.
.
.
x2n y2n z2n
un vn
.
.
.
u2n−2 v2n−2
Each coordinate , , and is an integer from to . Nodes within a single tree have distinct coordinates.
The pair means an edge exists between nodes and , where . For the first tree, . For the second tree, .
Output
If the two trees are twins, output the number of different node correspondences. Otherwise, output 0.