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. springcloud-Netflix创建服务消费者

    目录 springcloud-Netflix创建服务消费者 Ribbon 创建服务消费者-Ribbon方式 ribbon的架构 Feign 创建包和基本项目结构 创建Feign访问服务的接口和访问co ...

  2. WPF机制和原理

    最近由于项目需要,自己学习了一下WPF,之前接触过sliverlight,所以对理解和编写XAML不是太陌生.其实XAML和html多少还是有点类似的.只不过XAML上添加上了自动binding机制( ...

  3. JAVA变量介绍

    1.变量: 变量是内存中存储数据的小盒子(小容器),用来存数据和取数据: 2.计算机存储设备的最小信息单元叫位(bit   b); 计算机最小的存储单元叫字节(byte B);   存储单位有(bit ...

  4. 解决resteasy上传表单文件名乱码

    Dubbo在2.6版本后合并了dubbox的resteasy代码后,可以支持rest风格的接口发布,但是在使用form表单上传文件的时候,获取的文件名称是乱码. 下面通过对源码分析一下原因,并提供一种 ...

  5. js监听页面的scroll事件,当移到底部时触发事件

    //页面拉到底时自动加载更多 $(window).scroll(function(event){ var wScrollY = window.scrollY; // 当前滚动条位置 var wInne ...

  6. CF1142A The Beatles

    思路: 令p表示步数,l表示步长.由于p是使(l * p) % (n * k) == 0的最小的p,所以p = (n * k) / gcd(n * k, l). 设l = k * x + r,则由题意 ...

  7. Ubuntu获取root 权限,开机自动登入root

    新机器获取root权限,只需要给root 增加密码: sudo passwd root 修改开机自动登入: #sudo gedit /etc/lightdm/lightdm.conf 修改参数: au ...

  8. 【装载】删除Oracle11G

    卸载Oracle步骤:1.停止所有与ORACLE相关的服务.2. 使用OUI(Oracle Universal Installer)卸载Oracle软件.   “开始”->“程序”->“O ...

  9. 【Troubleshooting Case】Exchange Server 组件状态应用排错?

    在Exchange 2013中,引入了“服务器组件状态”的概念.服务器组件状态从运行环境的角度提供对组成Exchange Server的组件的状态的精细控制. 日常排错时,常常会把Exchange 服 ...

  10. WinForm 公共控件和属性

    Button  按钮 布局 AutoSize 内容超出部分是否扩展到适应尺寸大小 Location  位置坐标 Size   控件大小 行为 Enabled   控件是否启用 visible   控件 ...