Inaho VIII
View as PDFInaho was thinking of a tree problem, when he came up with this rather beautiful problem!
Given a tree originally rooted at containing
nodes each with a value
and an arbitrary value
, support
of following operations:
1 RReroot the tree so that nodeis the root.
2 a bPrint the highest common ancestor of nodesand
.
3 a bPrint the sum of all nodes'on the path from
to
, inclusive.
4 a bPrint the product of all nodes'on the path from
to
, inclusive, modulo
.
5 a bPrint the minimum of all nodes'on the path from
to
, inclusive.
6 a bPrint the maximum of all nodes'on the path from
to
, inclusive.
7 a bPrint the greatest common divisor of all nodes'on the path from
to
, inclusive.
8 a bPrint the bitwise AND of all nodes'on the path from
to
, inclusive.
9 a bPrint the bitwise OR of all nodes'on the path from
to
, inclusive.
10 a bPrint the bitwise XOR of all nodes'on the path from
to
, inclusive.
11 a bPrint the number of nodes whoseon the path from
to
, inclusive.
12 a bPrint the number of nodes whoseon the path from
to
, inclusive.
13 a bPrint the valuethat minimizes
, and
of all nodes on the path from
to
, inclusive. Print
if there is no such node where
.
14 a bPrint the valuethat minimizes
, and
of all nodes on the path from
to
, inclusive. Print
if there is no such node where
.
It is guaranteed .
Input Specification
The first line will contain three space-separated integers,
, the number of nodes, the number of operations, and the arbitrary value
, respectively.
The second line will contain space-separated integers,
, the values of each node.
The next lines will each contain two space-separated integers,
, indicating that nodes
and
are connected by an edge. It is guaranteed the entire tree is connected.
The next lines will each contain a valid operation as defined above.
Output Specification
For each operation that requires something to be outputted (everything except operation ), print the answer on its own line.
Sample Input
6 15 3
4 10 2 2 5 1
1 2
1 3
3 4
3 5
3 6
2 1 2
1 3
2 1 2
3 2 5
4 4 1
5 1 6
6 3 5
7 2 3
8 3 4
9 5 3
10 6 2
11 2 6
12 3 1
13 4 5
14 1 2
Sample Output
1
3
21
16
1
5
2
2
7
13
2
1
5
3
Comments