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

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("");
}
}
上一篇:使用Android网络编程实现简易聊天室


下一篇:P2774 方格取数问题(网络流)