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):

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXN = ;
const int MAXE = MAXN * ;
const int INF = 0x7fffffff; int ch[MAXN][], pre[MAXN], key[MAXN];
int add[MAXN], rev[MAXN], maxt[MAXN];
bool rt[MAXN]; int head[MAXN];
int to[MAXE], next[MAXE];
int ecnt, n, m, op; inline void init() {
ecnt = ;
for(int i = ; i <= n; ++i) {
head[i] = ch[i][] = ch[i][] = ;
pre[i] = add[i] = rev[i] = ;
rt[i] = true;
}
maxt[] = -INF;
} inline void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
} void dfs(int u) {
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(pre[v]) continue;
pre[v] = u;
dfs(v);
}
} inline void update_max(int r) {
maxt[r] = max(max(maxt[ch[r][]], maxt[ch[r][]]), key[r]);
} inline void update_add(int r, int d) {
if(!r) return ;
key[r] += d;
maxt[r] += d;
add[r] += d;
} inline void update_rev(int r) {
if(!r) return ;
swap(ch[r][], ch[r][]);
rev[r] ^= ;
} inline void pushdown(int r) {
if(add[r]) {
update_add(ch[r][], add[r]);
update_add(ch[r][], add[r]);
add[r] = ;
}
if(rev[r]) {
update_rev(ch[r][]);
update_rev(ch[r][]);
rev[r] = ;
}
} void rotate(int x) {
int y = pre[x], t = ch[y][] == x;
ch[y][t] = ch[x][!t];
pre[ch[y][t]] = y;
pre[x] = pre[y];
pre[y] = x;
ch[x][!t] = y;
if(rt[y]) rt[y] = false, rt[x] = true;
else ch[pre[x]][ch[pre[x]][] == y] = x;
update_max(y);
} void P(int r) {
if(!rt[r]) P(pre[r]);
pushdown(r);
} inline void Splay(int r) {
P(r);
while(!rt[r]) {
int f = pre[r], ff = pre[f];
if(rt[f]) rotate(r);
else if((ch[ff][] == f) == (ch[f][] == r)) rotate(f), rotate(r);
else rotate(r), rotate(r);
}
update_max(r);
} inline int access(int x) {
int y = ;
while(x) {
Splay(x);
rt[ch[x][]] = true, rt[ch[x][] = y] = false;
update_max(x);
x = pre[y = x];
}
return y;
} inline void be_root(int r) {
access(r);
Splay(r);
update_rev(r);
} inline void lca(int &u, int &v) {
access(v), v = ;
while(u) {
Splay(u);
if(!pre[u]) return ;
rt[ch[u][]] = true;
rt[ch[u][] = v] = false;
update_max(u);
u = pre[v = u];
}
} inline int root(int u) {
while(pre[u]) u = pre[u];
return u;
} inline void link(int u, int v) {
if(u == v || root(u) == root(v)) puts("-1");
else {
be_root(u);
pre[u] = v;
}
} inline void cut(int u, int v) {
if(u == v || root(u) != root(v)) puts("-1");
else {
be_root(u);
Splay(v);
pre[ch[v][]] = pre[v];
pre[v] = ;
rt[ch[v][]] = true;
ch[v][] = ;
update_max(v);
}
} inline void modity(int u, int v, int w) {
if(root(u) != root(v)) puts("-1");
else {
lca(u, v);
update_add(ch[u][], w);
update_add(v, w);
key[u] += w;
update_max(u);
}
} inline void query(int u, int v) {
if(root(u) != root(v)) puts("-1");
else {
lca(u, v);
printf("%d\n", max(max(maxt[v], maxt[ch[u][]]), key[u]));
}
} int main() {
while(scanf("%d", &n) != EOF) {
init();
for(int i = ; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
add_edge(u, v);
}
for(int i = ; i <= n; ++i) scanf("%d", &key[i]);
pre[] = -; dfs(); pre[] = ;
scanf("%d", &m);
while(m--) {
scanf("%d", &op);
if(op == ) {
int x, y;
scanf("%d%d", &x, &y);
link(x, y);
}
if(op == ) {
int x, y;
scanf("%d%d", &x, &y);
cut(x, y);
}
if(op == ) {
int x, y, w;
scanf("%d%d%d", &w, &x, &y);
modity(x, y, w);
}
if(op == ) {
int x, y;
scanf("%d%d", &x, &y);
query(x, y);
}
}
puts("");
}
}

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. papers-06-02

    午睡被同事吵醒,只好干活.看到微信公众号有一篇文章说老朋友呢,点进去发现原来相关的工作好多,而且好新好细致. 微信的文章可以见这里: 探究最陌生的老朋友Softmax 里面的几篇文章可以看看. Lar ...

  2. react setState修改嵌套对象

    在react使用setState过程中,难免会遇到复杂的数据类型,,这里还要提醒一下setState 是异步操作行为,需要setState之后的结果做为参数,请求数据的话,可以配合 async  aw ...

  3. CSS&JS定位器

    一.CssSelector定位器 1.概述 CssSelector是效率很高的元素定位方法,Selenium官网的Document里极力推荐使用CSS locator,而不是XPath来定位元素,原因 ...

  4. 简析--HashCode

    内容转载自:http://www.cnblogs.com/szlbm/p/5806226.html 哈希表 在了解HashCode之前,我们先来认识一下哈希表; 散列表(Hash table,也叫哈希 ...

  5. MySQL配置和启动

    1.首先下载MySQl安装包,解压安装包 打开mysql下面的bin文件夹,双击如图标记的  .exe运行 2.配置Mysql (1)运行程序后点击 next (2)这里有精确配置和标准配置,根据情况 ...

  6. xcode 快捷键大全、XCode常用快捷键图文介绍

    其实就是设置里面的快捷键变成了文字版,刚开始用Xcode是不是发现以前熟悉的开发环境的快捷键都不能用了?怎么快捷运行,停止,编辑等等.都不一样了.快速的掌握这些快捷键,能提供开发的效率. 其实快捷键在 ...

  7. js替换字符串中的空格,换行符\r\n或\n替换成<br>

    为了让回车换行符正确显示,需要将 \n 或 \r\n 替换成 <br>.同样地,将空格替换存  .这里我们通过正则表达式来替换. 一.替换所有的空格.回车换行符 //原始字符串 var s ...

  8. 【前行】◇第3站◇ Codeforces Round #512 Div2

    [第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...

  9. Maven里面多环境下的属性过滤(配置)

    情景:通常一个项目都为分为开发环境(dev)和测试环境(test)还有正式环境(prod),如果每次一打包都要手动地去更改配置文件,例如数据库连接配置.将会很容易出差错. 解决方案:maven pro ...

  10. CentOS查看占用端口并关闭

    1.查看占用的端口号 netstat -lnp|grep 80  #80 是你需要查看的端口号 二.查看进程的详细信息 ps 29280  #查看进行信息,是否是自己要找的进程 三.杀掉进程 kill ...