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. Java基础——数组复习

    数组是一个变量,存储相同数据类型的一组数据 声明一个变量就是在内存空间划出一块合适的空间 声明一个数组就是在内存空间划出一串连续的空间 数组长度固定不变,避免数组越界   数组是静态分配内存空间的,所 ...

  2. 菜鸟笔记 -- Chapter 6.2.3 成员变量

    6.2.3  成员变量 在Java中对象的属性也称为成员变量[也叫字段].成员变量的类型可以设置为Java中合法的数据类型,其实成员变量就是普通的变量,可以为它设置初始值,也可以不设置初始值,如果不设 ...

  3. springboot 整合dubbo 消费模块引入springboot 之后自动注入jdbc 模块导致启动报错问题

    方案一: 排除方法 pom文件直接将数据起步模块内排除数据源自动注入 jdbc jar <!--mybatis-plus 集成 --><!--mybitis--><dep ...

  4. SQL里的concat() 以及group_concat() 函数的使用

    实例参考:https://blog.csdn.net/mary19920410/article/details/76545053 一 concat()函数 1.功能:将多个字符串连接成一个字符串. 2 ...

  5. 【TOJ 3660】家庭关系(hash+并查集)

    描述 给定若干家庭成员之间的关系,判断2个人是否属于同一家庭,即2个人之间均可以通过这些关系直接或者间接联系. 输入 输入数据有多组,每组数据的第一行为一个正整数n(1<=n<=100), ...

  6. Flask中对MySQL的基本操作

    在Flask-SQLAlchemy中,插入.修改.删除操作,均由数据库会话管理. 会话用 db.session 表示.在准备把数据写入数据库前,要先将数据添加到会话中然后调用 commit() 方法提 ...

  7. datatable根据条件设置表格行中字的颜色或背景

    使用row回调函数 "rowCallback": function (row, data) { if (xxx) { //给行添加背景色 $(row).css("back ...

  8. 禁止鼠标点右键 - 防止刷新页面 - 禁止复制 chrome 和 firefox不能复制

    document.oncontextmenu = function () {//点右键,啥反应都没有了 return false; } document.onkeydown = function () ...

  9. vertical-align垂直居中

    <div id="content"> <div id="weizi"> 锄禾日当午,<br> 汗滴禾下土.<br> ...

  10. 强化记忆之php

    php 输出的区分 新手摸索道路,有说不对的地方,还请多多包涵. echo 能够输出一个以上的字符串,也能输出html标签 print  一次只能接受一个字符串(区分与echo),也能输出html标签 ...