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.

题目大意:给出一棵带点权树,有4种操作:连边x到y;删掉以x为根的树种y与其父节点的连边;把x到y路径上所有点的权值+1;询问x到y路径上的最大点权。

推荐论文:《QTREE解法的一些研究》,参考代码:http://www.cnblogs.com/kuangbin/archive/2013/09/04/3300251.html

代码(1156MS):

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. const int MAXN = ;
  8. const int MAXE = MAXN * ;
  9. const int INF = 0x7fffffff;
  10.  
  11. int ch[MAXN][], pre[MAXN], key[MAXN];
  12. int add[MAXN], rev[MAXN], maxt[MAXN];
  13. bool rt[MAXN];
  14.  
  15. int head[MAXN];
  16. int to[MAXE], next[MAXE];
  17. int ecnt, n, m, op;
  18.  
  19. inline void init() {
  20. ecnt = ;
  21. for(int i = ; i <= n; ++i) {
  22. head[i] = ch[i][] = ch[i][] = ;
  23. pre[i] = add[i] = rev[i] = ;
  24. rt[i] = true;
  25. }
  26. maxt[] = -INF;
  27. }
  28.  
  29. inline void add_edge(int u, int v) {
  30. to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
  31. to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
  32. }
  33.  
  34. void dfs(int u) {
  35. for(int p = head[u]; p; p = next[p]) {
  36. int &v = to[p];
  37. if(pre[v]) continue;
  38. pre[v] = u;
  39. dfs(v);
  40. }
  41. }
  42.  
  43. inline void update_max(int r) {
  44. maxt[r] = max(max(maxt[ch[r][]], maxt[ch[r][]]), key[r]);
  45. }
  46.  
  47. inline void update_add(int r, int d) {
  48. if(!r) return ;
  49. key[r] += d;
  50. maxt[r] += d;
  51. add[r] += d;
  52. }
  53.  
  54. inline void update_rev(int r) {
  55. if(!r) return ;
  56. swap(ch[r][], ch[r][]);
  57. rev[r] ^= ;
  58. }
  59.  
  60. inline void pushdown(int r) {
  61. if(add[r]) {
  62. update_add(ch[r][], add[r]);
  63. update_add(ch[r][], add[r]);
  64. add[r] = ;
  65. }
  66. if(rev[r]) {
  67. update_rev(ch[r][]);
  68. update_rev(ch[r][]);
  69. rev[r] = ;
  70. }
  71. }
  72.  
  73. void rotate(int x) {
  74. int y = pre[x], t = ch[y][] == x;
  75. ch[y][t] = ch[x][!t];
  76. pre[ch[y][t]] = y;
  77. pre[x] = pre[y];
  78. pre[y] = x;
  79. ch[x][!t] = y;
  80. if(rt[y]) rt[y] = false, rt[x] = true;
  81. else ch[pre[x]][ch[pre[x]][] == y] = x;
  82. update_max(y);
  83. }
  84.  
  85. void P(int r) {
  86. if(!rt[r]) P(pre[r]);
  87. pushdown(r);
  88. }
  89.  
  90. inline void Splay(int r) {
  91. P(r);
  92. while(!rt[r]) {
  93. int f = pre[r], ff = pre[f];
  94. if(rt[f]) rotate(r);
  95. else if((ch[ff][] == f) == (ch[f][] == r)) rotate(f), rotate(r);
  96. else rotate(r), rotate(r);
  97. }
  98. update_max(r);
  99. }
  100.  
  101. inline int access(int x) {
  102. int y = ;
  103. while(x) {
  104. Splay(x);
  105. rt[ch[x][]] = true, rt[ch[x][] = y] = false;
  106. update_max(x);
  107. x = pre[y = x];
  108. }
  109. return y;
  110. }
  111.  
  112. inline void be_root(int r) {
  113. access(r);
  114. Splay(r);
  115. update_rev(r);
  116. }
  117.  
  118. inline void lca(int &u, int &v) {
  119. access(v), v = ;
  120. while(u) {
  121. Splay(u);
  122. if(!pre[u]) return ;
  123. rt[ch[u][]] = true;
  124. rt[ch[u][] = v] = false;
  125. update_max(u);
  126. u = pre[v = u];
  127. }
  128. }
  129.  
  130. inline int root(int u) {
  131. while(pre[u]) u = pre[u];
  132. return u;
  133. }
  134.  
  135. inline void link(int u, int v) {
  136. if(u == v || root(u) == root(v)) puts("-1");
  137. else {
  138. be_root(u);
  139. pre[u] = v;
  140. }
  141. }
  142.  
  143. inline void cut(int u, int v) {
  144. if(u == v || root(u) != root(v)) puts("-1");
  145. else {
  146. be_root(u);
  147. Splay(v);
  148. pre[ch[v][]] = pre[v];
  149. pre[v] = ;
  150. rt[ch[v][]] = true;
  151. ch[v][] = ;
  152. update_max(v);
  153. }
  154. }
  155.  
  156. inline void modity(int u, int v, int w) {
  157. if(root(u) != root(v)) puts("-1");
  158. else {
  159. lca(u, v);
  160. update_add(ch[u][], w);
  161. update_add(v, w);
  162. key[u] += w;
  163. update_max(u);
  164. }
  165. }
  166.  
  167. inline void query(int u, int v) {
  168. if(root(u) != root(v)) puts("-1");
  169. else {
  170. lca(u, v);
  171. printf("%d\n", max(max(maxt[v], maxt[ch[u][]]), key[u]));
  172. }
  173. }
  174.  
  175. int main() {
  176. while(scanf("%d", &n) != EOF) {
  177. init();
  178. for(int i = ; i < n; ++i) {
  179. int u, v;
  180. scanf("%d%d", &u, &v);
  181. add_edge(u, v);
  182. }
  183. for(int i = ; i <= n; ++i) scanf("%d", &key[i]);
  184. pre[] = -; dfs(); pre[] = ;
  185. scanf("%d", &m);
  186. while(m--) {
  187. scanf("%d", &op);
  188. if(op == ) {
  189. int x, y;
  190. scanf("%d%d", &x, &y);
  191. link(x, y);
  192. }
  193. if(op == ) {
  194. int x, y;
  195. scanf("%d%d", &x, &y);
  196. cut(x, y);
  197. }
  198. if(op == ) {
  199. int x, y, w;
  200. scanf("%d%d%d", &w, &x, &y);
  201. modity(x, y, w);
  202. }
  203. if(op == ) {
  204. int x, y;
  205. scanf("%d%d", &x, &y);
  206. query(x, y);
  207. }
  208. }
  209. puts("");
  210. }
  211. }

HDU 4010 Query on The Trees(动态树LCT)的更多相关文章

  1. HDU 4010 Query on The Trees (动态树)(Link-Cut-Tree)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意; 先给你一棵树,有 \(4\) 种操作: 1.如果 \(x\) 和 \(y\) 不在同一 ...

  2. HDU 4010 Query on The Trees(动态树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意:一棵树,四种操作: (1)若x和y不在一棵树上,将x和y连边: (2)若x和y在一棵树上, ...

  3. 动态树(LCT):HDU 4010 Query on The Trees

    Query on The Trees Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Othe ...

  4. HDU 4010.Query on The Trees 解题报告

    题意: 给出一颗树,有4种操作: 1.如果x和y不在同一棵树上则在xy连边 2.如果x和y在同一棵树上并且x!=y则把x换为树根并把y和y的父亲分离 3.如果x和y在同一棵树上则x到y的路径上所有的点 ...

  5. HDU 4010 Query on The Trees(动态树)

    题意 给定一棵 \(n\) 个节点的树,每个点有点权.完成 \(m\) 个操作,操作四两种,连接 \((x,y)\) :提 \(x\) 为根,并断 \(y\) 与它的父节点:增加路径 \((x,y)\ ...

  6. HDU 4010 Query on The Trees

    Problem Description We have met so many problems on the tree, so today we will have a query problem ...

  7. hdu 4010 Query on The Trees LCT

    支持:1.添加边 x,y2.删边 x,y3.对于路径x,y上的所有节点的值加上w4.询问路径x,y上的所有节点的最大权值 分析:裸的lct...rev忘了清零死循环了两小时... 1:就是link操作 ...

  8. hdu 5398 动态树LCT

    GCD Tree Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  9. hdu 5002 (动态树lct)

    Tree Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

随机推荐

  1. spring cloud 学习之路由网关(zuul)

    学习自方志朋的博客 http://blog.csdn.net/forezp/article/details/69939114 在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费. ...

  2. .scripts/mysql_install_db: 没有那个文件或目录

    .scripts/mysql_install_db: 没有那个文件或目录 查了好多地方,在书上找到了解决方案,太不容易了 原因与解决方法: 系统与MYSQL版本不同,系统64位使用64位MYSQL,3 ...

  3. 【Win10分区教程】

    Win10怎么分区?如何为Win10硬盘分区? 注:本教程适用于Win7.Win8.Win8.1和Win10系 到了Windows10时代,TB级硬盘已经很普及了,那么在Win10系统下如何为这些大容 ...

  4. javascript--淘宝页面的放大镜效果

    放大镜效果需求: 鼠标放入原图中,会出现一个黄色的遮盖层和一个放大的图片,鼠标移动时候,遮盖层会跟着鼠标一起移动,同时放大的图片会跟着一起移动. 实现过程: 1.鼠标移入,遮盖层和大图片显示 2.鼠标 ...

  5. dom技术解析xml (php)

    1.xml实例 test.xml <?xml version="1.0" encoding="utf-8"?><!DOCTYPE 班级 SYS ...

  6. js根据年份获取某月份有几天

    function getNum(year, month) { var temp; month = parseInt(month, 10); temp = new Date(year, month, 0 ...

  7. Mina 组件介绍之 IoBuffer

    在Java NIO 中,ByteBuffer通常作为通信中传递消息的载体.而在Mina中,采用了IoBuffer代替ByteBuffer.Mina给出了不用ByteBuffer的两个主要理由: 1.  ...

  8. MySQL数据操作(DML)

    表结构准备: mysql> CREATE TABLE student( -> sid INT PRIMARY KEY AUTO_INCREMENT, ), -> age INT, ) ...

  9. Java学习笔记十一:Java中的方法

    Java中的方法 一:什么是方法: 所谓方法,就是用来解决一类问题的代码的有序组合,是一个功能模块. 学过C语言或者其他语言的应该都知道函数这个东西,在Java中,其实方法就是函数,只不过叫法不同,在 ...

  10. C语言常用关键语法精华总结

    1.关于typedef的用法总结 2.typedef struct的用法 3.typedef函数指针用法 4.数组指针(数组类型的指针)与指针数组 5.真正明白c语言二级指针 6.C语言for循环(及 ...