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. webpack.config.js====插件html-webpack-plugin

    1. 安装 cnpm install html-webpack-plugin --save-dev 2. webpack.config.js中使用 const htmlWebpackPlugin = ...

  2. logback-spring.xml

    <?xml version="1.0" encoding="UTF-8"?><!--该日志将日志级别不同的log信息保存到不同的文件中--&g ...

  3. 准备Kendo UI 开发环境

    准备 首先你需要从 Telerik 网站下载试用版开发包,注意需要注册后才能下载. 下载后直接解压后包含下面几个文件和目录: ./examples – 示例. /js – minified 化后的 J ...

  4. mac不限速下载百度网盘

    本文转载自:https://blog.csdn.net/u010837612/article/details/80029212 相信大家都比较困惑,百度网盘客户端限速后一般只有几十K的下载速度,Win ...

  5. Windows Azure 配置Active Directory 主机(2)

    前一篇概况给大家介绍了,在云端部署一台DC 需要满足一些条件,接下来进入正题,云端VM安装域控制器具体步骤. 步骤1 :验证 主DC 的静态 IP 地址 1.登录到 Corp 网络上的 主DC. 2. ...

  6. less的使用总结

    简单执行less 一.使用npm全局安装less: npm install -g less 二.创建less文件 三.执行命令将less文件转换成css文件 lessc less/style.less ...

  7. 【LeetCode】9 Palindrome Number 回文数判定

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  8. div高度不能自适应(子级使用float浮动,父级div高度不能自适应)

    1.问题截图: 2.问题描述: 由于地址.公司名长度的不定性,所以每一条地址所在的父级div高度不定,但是需要设置一个最小的高度min-height:48px;但是当内容增加的时候,父级div高度却不 ...

  9. Tarjan的学习笔记 求割边求割点

    博主图论比较弱,搜了模版也不会用... 所以决心学习下tarjan算法. 割点和割边的概念不在赘述,tarjan能在线性时间复杂度内求出割边. 重要的概念:时间戟,就是一个全局变量clock记录访问结 ...

  10. JavaScript BOM DOM 对象

    title: JavaScript BOM DOM 对象 tags: JavaScript --- browser object model document onject model BOM对象 w ...