Query on The Trees

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 6447    Accepted Submission(s):
2547

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

5
1 2
2 4
2 5
1 3
1 2 3 4 5
6
4 2 3
2 1 2
4 2 3
1 3 5
3 2 1 4
4 1 4
 

Sample Output

3
-1
7

 

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.

 

题意

给出一颗树,有4种操作:

  1. 如果x和y不在同一棵树上,则在x,y之间连一条边
  2. 如果x和y在同一棵树上,并且x!=y,则把x换为树根,并把y和其父亲分离
  3. 如果x和y在同一棵树上,则x到y的路径上所有的点权值加上w
  4. 如果x和y在同一棵树上,则输出x到y路径上的最大值

code

LCT —— 神奇的数据结构

 #include<cstdio>
#include<algorithm>
#include<cstring> using namespace std; const int N = ;
int ch[N][],fa[N],val[N],add[N],rev[N],mx[N],head[N];
int st[N],top,n,m,tot;
struct Edge{
int to,nxt;
}e[N<<]; inline int read() {
int x = ,f = ;char ch = getchar();
for (; ch<''||ch>''; ch = getchar()) if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = getchar()) x = x * + ch - '';
return x * f;
}
void add_edge(int u,int v) {
e[++tot].to = v,e[tot].nxt = head[u],head[u] = tot;
}
void pushup(int x) {
mx[x] = max(max(mx[ch[x][]],mx[ch[x][]]),val[x]);
}
void pushdown(int x) {
int l = ch[x][],r = ch[x][];
if (rev[x]) {
rev[l] ^= ;rev[r] ^= ;
swap(ch[x][],ch[x][]);
rev[x] ^= ;
}
if (add[x]) {
if (l) add[l] += add[x],mx[l] += add[x],val[l] += add[x];
if (r) add[r] += add[x],mx[r] += add[x],val[r] += add[x];
add[x] = ;
}
}
bool isroot(int x) {
return ch[fa[x]][]!=x && ch[fa[x]][]!=x;
}
inline int son(int x) {
return ch[fa[x]][]==x;
}
void rotate(int x) {
int y = fa[x],z = fa[y],b = son(x),c = son(y),a = ch[x][!b];
if (!isroot(y)) ch[z][c] = x;fa[x] = z;
ch[x][!b] = y;fa[y] = x;
ch[y][b] = a;if (a) fa[a] = y;
pushup(y);pushup(x);
}
void splay(int x) {
top = ;st[++top] = x;
for (int i=x; !isroot(i); i=fa[i]) st[++top] = fa[i];
while (top) pushdown(st[top--]);
while (!isroot(x)) {
int y = fa[x];
if (!isroot(y)) {
if (son(x)==son(y)) rotate(y);
else rotate(x);
}
rotate(x);
}
}
void access(int x) {
for (int t=; x; t=x,x=fa[x]) {
splay(x);ch[x][] = t;pushup(x);
}
}
void makeroot(int x) {
access(x);splay(x);rev[x] ^= ;
}
void link(int x,int y) {
makeroot(x);fa[x] = y;
}
void cut(int x,int y) {
makeroot(x);access(y);splay(y);
ch[y][] = fa[ch[y][]] = ;pushup(y);
}
int find(int x) {
access(x);splay(x);
while (ch[x][]) x = ch[x][];
return x;
}
void update(int x,int y,int z) {
makeroot(x);access(y);splay(y);
add[y] += z;mx[y] += z;val[y] += z;
}
int query(int x,int y) {
makeroot(x);access(y);splay(y);
return mx[y];
}
int main() {
while (scanf("%d",&n) != EOF) {
for (int i=; i<=n; ++i)
head[i] = add[i] = rev[i] = fa[i] = ch[i][] = ch[i][] = ;
mx[] = -1e9;tot = ;
for (int a,b,i=; i<n; ++i) {
a = read();b = read();
add_edge(a,b);add_edge(b,a);
}
for (int i=; i<=n; ++i) mx[i] = val[i] = read();
st[++top] = ;
for (int k=; k<=top; ++k) {
int u = st[k];
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (v != fa[u]) {
fa[v] = u;st[++top] = v;
}
}
}
m = read();
while (m--) {
int opt = read(),x = read(),y = read(),w;
if (opt==) {
if (find(x) == find(y)) puts("-1");
else link(x,y);
}
else if (opt==) {
if (find(x) != find(y) || x==y) puts("-1");
else cut(x,y);
}
else if (opt==) {
w = x;x = y;y = read();
if (find(x) != find(y)) puts("-1");
else update(x,y,w);
}
else {
if (find(x) != find(y)) puts("-1");
else printf("%d\n",query(x,y));
}
}
puts("");
}
return ;
}

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

  1. Hdu 4010-Query on The Trees LCT,动态树

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

  2. HDU4010 Query on The Trees(LCT)

    人生的第一道动态树,为了弄懂它的大致原理,需要具备一些前置技能,如Splay树,树链剖分的一些概念.在这里写下一些看各种论文时候的心得,下面的代码是拷贝的CLJ的模板,别人写的模板比较可靠也方便自己学 ...

  3. 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 ...

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

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

  5. SPOJ 375. Query on a tree (动态树)

    375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...

  6. HDOJ 4010 Query on The Trees LCT

    LCT: 分割.合并子树,路径上全部点的点权添加一个值,查询路径上点权的最大值 Query on The Trees Time Limit: 10000/5000 MS (Java/Others)   ...

  7. LCT 动态树 模板

    洛谷:P3690 [模板]Link Cut Tree (动态树) /*诸多细节,不注意就会调死去! 见注释.*/ #include<cstdio> #include<iostream ...

  8. [HNOI2010]弹飞绵羊 (平衡树,LCT动态树)

    题面 题解 因为每个点都只能向后跳到一个唯一的点,但可能不止一个点能跳到后面的某个相同的点, 所以我们把它抽象成一个森林.(思考:为什么是森林而不是树?) 子节点可以跳到父节点,根节点再跳就跳飞了. ...

  9. Fzu Problem 2082 过路费 LCT,动态树

    题目:http://acm.fzu.edu.cn/problem.php?pid=2082 Problem 2082 过路费 Accept: 528    Submit: 1654Time Limit ...

随机推荐

  1. 安卓H5软键盘遮挡输入框

    由于安卓app内嵌入H5页面,webview对于软键盘没有处理(如果不是产品强烈要求建议不要处理这种拆东墙补西墙的问题,因为其他的手机上可能会出现已经优化软键盘的情况) 1.input下方还有多余空位 ...

  2. eclipse查看jar包源文件

    话不多说上链接 https://www.cnblogs.com/1995hxt/p/5252098.html这里介绍了完整的流程,亲自试过,可以的! 以防以后要用的时候找不到文件的下载地址,所以就先在 ...

  3. Android开发学习——小细节注意

    Android中通过Intent调用其他应用的方法(转) Android中两种序列化方式的比较Serializable和Parcelable http://www.jcodecraeer.com/a/ ...

  4. Python+selenium之窗口截图

    自动化用例是由程序去执行,因此有时候打印的错误信息并不明确,如果在脚本执行错误的时候能对当前窗口截图保存,那么通过图片就可以非常直观的看出出错的原因.webdriver提供了截图函数get_scree ...

  5. python基础教程总结5——函数

    1.函数创建 1).函数代码块以def关键词开头,后接函数标识符名称和圆括号()2).任何传入参数和自变量必须放在圆括号中间.圆括号之间可以用于定义参数3).函数的第一行语句可以选择性地使用文档字符串 ...

  6. ansys-表格

    转自http://blog.sina.com.cn/s/blog_833dee820102xwb3.html ANSYS中表格数组的定义及使用举例 ANSYS中会有许多的参数数据,这些参数的形成后要放 ...

  7. UVA 11468 Substring (AC自动机)

    用把失配边也加到正常边以后AC自动机,状态是长度递减的DAG,每次选一个不会匹配字符的转移. dp[u][L]表示当前在tire树上u结点长度还剩L时候不匹配的概率,根据全概率公式跑记忆化搜索. #i ...

  8. 2018.3.11 Java DEBUG 调试的方法

    F5:Step Into:跳进当前执行的方法中 F6:Step Over:跳过该行继续执行 F7:Step Return:从方法中跳出继续执行 F8:Resume:全部运行通过,从调试状态恢复 Ctr ...

  9. 数据类型-------JavaScript

    之前只是简单的学过JavaScript和JQuery,虽然一般的要求都能完成,但并没有深入,这次是看了一个网站,很详细的教学,想重新认识一下JavaScript和JQuery. 本文摘要:http:/ ...

  10. shell脚本,awk取中间列的方法。

    解释 1.$(int(NF/2)+1) 中int(NF/2)等于3,然后加1,就得到中间的4了. 2.$(NF/2+0.5) 相当于得出的是整数.NF/2是3.5,再由3.5+0.5,所以就是4了,也 ...