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. SyntaxError: Use of const in strict mode.

    具体报错console c:\Users\Administrator\WebstormProjects\blogtest\node_modules\connect-mongo\src\index.js ...

  2. datatables后台分页例子(可直接复制代码)

    1.head表签引用 这两个文件即可 2.复制下面的代码到webform中的head标签中 <script> $(function () { //提示信息 var lang = { &qu ...

  3. ThreadLocal源码解析,内存泄露以及传递性

    我想ThreadLocal这东西,大家或多或少都了解过一点,我在接触ThreadLocal的时候,觉得这东西很神奇,在网上看了很多博客,也看了一些书,总觉得有一个坎跨不过去,所以对ThreadLoca ...

  4. 洛谷P2430 严酷的训练

    第一眼看这道题...啊哈,啥??? 仔细看一看,发现:诶, 这不是01背包吗? 两人水平值的比值*老王做题用时 可以算出WKY做每道题的用时. 那么每道题的p就可以转换成费用c[i], 价值q就是w[ ...

  5. iOS 应用架构 (三)

    iOS 客户端应用架构看似简单,但实际上要考虑的事情不少.本文作者将以系列文章的形式来回答 iOS 应用架构中的种种问题,本文是其中的第二篇,主要讲 View 层的组织和调用方案.下篇主要讨论做 Vi ...

  6. GIT SSH免登录密码实现更新(git pull)、推送(git push)操作

     一.使用场景 现在有两台服务器A和B,在A服务器上搭建有git版本代码仓库,现要实现B服务器SSH免密码登录A服务器,并能够从A服务器拉取.推送代码! 二.操作步骤 1.在B服务器项目根目录下执行以 ...

  7. Android商城开发系列(一)——开篇

    最近在看尚硅谷的硅谷商城视频,想系统学习一下Android的商城开发流程,打算跟着视频的一步步做出一个商城,然后写博客总结记录一下整个商城的开发过程以及使用到的技术知识点,这个商城的最终效果如下图所示 ...

  8. [会员登入] 透过 E-Mail进行身份认证、启用会员权利

    [會員登入] 透過 E-Mail進行身份認證.啟用會員權利 这个问题是在论坛上看见的 其实,遇见问题的时候,我会建议初学者先想一下「流程」 您想怎么作?需要哪些步骤? 一旦「流程」清楚了 您是哪一步骤 ...

  9. (外挂破解)Cheat Engine(内存修改工具)V6.2中文版软件介绍

    Heat Engine是一款内存修改编辑工具,Cheat Engine允许你修改你的游戏,所以你将总是赢.它包括16进制编辑,反汇编程序,内存查找工具.与同类修改工具相比,它具有强大的反汇编功能,且自 ...

  10. UI的组织形式

    UI的组织形式是树状结构: 根据层次的不同分为叶子节点和干节点. 叶子节点负责简单的信息展示. 复杂的主干复杂叶子节点的组织和整体展示. http://www.cnblogs.com/feng9exe ...