Problem
Tree
Description
Zero and One are good friends who always have fun with each other.
This time, they decide to do something on a tree which is a kind of graph that there is only one path from node to node. First, Zero will give One an tree and every node in this tree has a value. Then, Zero will ask One a series of queries. Each query contains three parameters: , , which mean that he want to know the maximum value produced by each value on the path from node to node (include node , node ). Unfortunately, One has no idea in this question. So he need you to solve it.
Input
There are several test cases and the cases end with . For each case:
The first line contains two integers and , which are the amount of tree’s nodes and queries, respectively.
The second line contains integers and is the value on the node.
The next lines contains two integers , which means there is an connection between and .
The next m lines contains three integers , which are the parameters of Zero’s query.
Output
For each query, output the answer.
Sample Input
1 | 3 2 |
Sample Output
1 | 3 |
Translation
给出一棵树,求两点间树上链路中的数异或得到的最大结果。
标签:LCA
可持久化Trie
Solution
最大异或和上树…
有异或,一个经典的解法就是建一棵字典树,然后贪心跑一遍,尽量选和给出数当前位不同的数。
对于树上的此类问题,可以把Trie树
可持久化,对于每个点,存它到根节点的路径上所有数的Trie树
,这样是有前缀和性质的,求后作差即可求出链路上的数。
可持久化方法和主席树差不多。
Code
1 |
|