【hdu4010】Query on The Trees

Problem Description
We have met so many problems on the tree, so today we will have a query problem on a set of trees.
There are N nodes, each node will have a unique weight Wi. We will have four kinds of operations on it and you should solve them efficiently. Wish you have fun!


There are multiple test cases in our dataset.
For each case, the first line contains only one integer N.(1 ≤ N ≤ 300000) The next N‐1 lines each contains two integers x, y which means there is an edge between them. It also means we will give you one tree initially.
The next line will contains N integers which means the weight Wi of each node. (0 ≤ Wi ≤ 3000)
The next line will contains an integer Q. (1 ≤ Q ≤ 300000) The next Q lines will start with an integer 1, 2, 3 or 4 means the kind of this operation.
1. Given two integer x, y, you should make a new edge between these two node x and y. So after this operation, two trees will be connected to a new one.
2. Given two integer x, y, you should find the tree in the tree set who contain node x, and you should make the node x be the root of this tree, and then you should cut the edge between node y and its parent. So after this operation, a tree will be separate into two parts.
3. Given three integer w, x, y, for the x, y and all nodes between the path from x to y, you should increase their weight by w.
4. Given two integer x, y, you should check the node weights on the path between x and y, and you should output the maximum weight on it.
For each query you should output the correct answer of it. If you find this query is an illegal operation, you should output ‐1.
You should output a blank line after each test case.
Sample Input
5 1 2 2 4 2 5 1 3 1 2 3 4 5 6 4 2 3 2 1 2 4 2 3 1 3 5 3 2 1 4 4 1 4
Sample Output
3 -1 7


We define the illegal situation of different operations: In first operation: if node x and y belong to a same tree, we think it’s illegal. In second operation: if x = y or x and y not belong to a same tree, we think it’s illegal. In third operation: if x and y not belong to a same tree, we think it’s illegal. In fourth operation: if x and y not belong to a same tree, we think it’s illegal.





  • 尹一航2015年1月30日 下午1:42 回复


    • 李雪欣2015年1月30日 下午2:49 回复


      • 尹一航2015年1月30日 下午4:36 回复


        • 魔法炮2015年4月9日 上午8:20 回复


          • 李雪欣2015年4月9日 上午8:22


          • 吴一凡2015年4月9日 下午4:26


          • 魔法炮2015年4月9日 下午6:33


        • 魔法炮2015年4月9日 上午8:22 回复


  • syk_____2015年4月8日 下午8:56 回复

    cut之前为什么要makeroot一次啊 感觉可以不用的

  • 吴钰晗2015年4月10日 下午2:08 回复


  • 吴钰晗2015年4月10日 下午2:08 回复


  • ssqqq2016年2月17日 下午6:11 回复


    • hzwer2016年2月18日 上午12:10 回复


      • ssqqq2016年2月18日 下午12:15 回复