|
||||||||||
Query on The TreesTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 8411 Accepted Submission(s): 3124 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! Input 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. Output 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
Sample Output
Hint 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. Source | ||||||||||
|