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
 
LCT模板
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MN 300001
using namespace std; int p,ca,f;
inline int read(){
p=;ca=getchar();f=;
while(ca<''||ca>'') {if (ca=='-') f=-;ca=getchar();}
while(ca>=''&&ca<='') p=p*+ca-,ca=getchar();
return p*f;
}
struct na{
int y,ne;
}b[MN*];
int fa[MN],n,m,x,y,ch[MN][],top,st[MN],key[MN],ma[MN],c[MN],l[MN],r[MN],num;
bool rev[MN];
inline int max(int a,int b){return a>b?a:b;}
inline bool isroot(int x){
return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;
}
inline void pu(int x,int w){
if (!x) return;
c[x]+=w;key[x]+=w;ma[x]+=w;
}
inline void pd(int x){
if (c[x]){
pu(ch[x][],c[x]);pu(ch[x][],c[x]);
c[x]=;
}
if (rev[x]){
rev[x]=;rev[ch[x][]]^=;rev[ch[x][]]^=;
swap(ch[x][],ch[x][]);
}
}
inline void up(int x){
pd(x);pd(ch[x][]);pd(ch[x][]);
ma[x]=max(max(ma[ch[x][]],ma[ch[x][]]),key[x]);
}
inline void rot(int x){
int y=fa[x],kind=ch[y][]==x;
if(!isroot(y)) ch[fa[y]][ch[fa[y]][]==y]=x;
fa[x]=fa[y];
fa[y]=x;
ch[y][kind]=ch[x][!kind];
fa[ch[y][kind]]=y;
ch[x][!kind]=y;
up(y);up(x);
}
inline void splay(int x){
register int i;top=;st[]=x;
for (i=x;!isroot(i);i=fa[i]) st[++top]=fa[i];
for (i=top;i;i--) up(st[i]);
while(!isroot(x)){
if (isroot(fa[x])) rot(x);else
if ((ch[fa[fa[x]]][]==fa[x])==(ch[fa[x]][]==x)) rot(fa[x]),rot(x);else rot(x),rot(x);
}
}
inline void acc(int u){
int x=;
while(u){
splay(u);
ch[u][]=x;
u=fa[x=u];
}
}
inline int find(int x){
acc(x);splay(x);
while(ch[x][]) x=ch[x][];
return x;
}
inline bool qu(int x,int y){
if (find(x)==find(y)) return ;else return ;
}
inline void re(int x){
acc(x);splay(x);rev[x]^=;
}
inline void in(int x,int y){
if (qu(x,y)){printf("-1\n");return;}
re(x);acc(y);
ch[y][]=x;fa[x]=y;
}
inline void del(int x,int y){
if (!qu(x,y)||x==y){printf("-1\n");return;}
re(x);acc(y);splay(y);ch[y][]=fa[ch[y][]]=;
}
inline void change(int x,int y,int w){
if (!qu(x,y)){printf("-1\n");return;}
re(x);acc(y);splay(y);pu(y,w);
}
inline int que(int x,int y){
if (!qu(x,y)) return -;
re(x);acc(y);splay(y);
return ma[y];
}
inline void inl(int x,int y){
num++;
if (!l[x]) l[x]=num;else b[r[x]].ne=num;
b[num].y=y;b[num].ne=;r[x]=num;
}
inline void dfs(int x){
for (int i=l[x];i;i=b[i].ne)
if (!fa[b[i].y]){
fa[b[i].y]=x;
dfs(b[i].y);
}
}
int o;
int main(){
register int i;
ma[]=-1e9;
while(scanf("%d",&n)!=EOF){
num=;
memset(fa,,sizeof(fa));
memset(ch,,sizeof(ch));
memset(c,,sizeof(c));
memset(l,,sizeof(l));
memset(rev,,sizeof(rev));
for (i=;i<n;i++){
x=read();y=read();
inl(x,y);
inl(y,x);
}
for(i=;i<=n;i++) ma[i]=key[i]=read();
fa[]=;
dfs();
fa[]=;
m=read();
while(m--){
o=read();
int x,y,z;x=read();y=read();
if (o==) in(x,y);else
if (o==) del(x,y);else
if (o==) z=read(),change(y,z,x);else
printf("%d\n",que(x,y));
}
printf("\n");
}
}

HDU 4010 Query on The Trees的更多相关文章

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

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

  2. HDU 4010 Query on The Trees (动态树)(Link-Cut-Tree)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意; 先给你一棵树,有 \(4\) 种操作: 1.如果 \(x\) 和 \(y\) 不在同一 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意:一棵树,四种操作: (1)若x和y不在一棵树上,将x和y连边: (2)若x和y在一棵树上, ...

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

  5. HDU 4010.Query on The Trees 解题报告

    题意: 给出一颗树,有4种操作: 1.如果x和y不在同一棵树上则在xy连边 2.如果x和y在同一棵树上并且x!=y则把x换为树根并把y和y的父亲分离 3.如果x和y在同一棵树上则x到y的路径上所有的点 ...

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

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

  7. hdu 4010 Query on The Trees LCT

    支持:1.添加边 x,y2.删边 x,y3.对于路径x,y上的所有节点的值加上w4.询问路径x,y上的所有节点的最大权值 分析:裸的lct...rev忘了清零死循环了两小时... 1:就是link操作 ...

  8. HDOJ 4010 Query on The Trees LCT

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

  9. HDU 5957 Query on a graph

    HDU 5957 Query on a graph 2016ACM/ICPC亚洲区沈阳站 题意 \(N(N \le 10^5)\)个点,\(N\)条边的连通图. 有\(M \le 10^5\)操作: ...

随机推荐

  1. iOS 蓝牙开发资料记录

    一.蓝牙基础认识:   1.iOS蓝牙开发:  iOS蓝牙开发:蓝牙连接和数据读写   iOS蓝牙后台运行  iOS关于app连接已配对设备的问题(ancs协议的锅)          iOS蓝牙空中 ...

  2. iOS控制器跳转动画

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 MyViewController *myVC = [[MyViewController alloc]init];  //创建动画  C ...

  3. mybatis的那些事

    转载请注明出处:http://www.cnblogs.com/yidaijiankuanzhongbuhui/p/7003993.html 用mybatis一年多了,一直是别人搭好框架,配置好各种配置 ...

  4. xamarin android menu的用法

    在Android中的菜单有如下几种: OptionMenu:选项菜单,android中最常见的菜单,通过Menu键来调用 SubMenu:子菜单,android中点击子菜单将弹出一个显示子菜单项的悬浮 ...

  5. 一个APP页面一个接口

    目前所在的公司做的是健康产业方面的APP,这个产品包括了安卓和IOS还有web三方面,除了要写后台管理的系统外,还要写移动端的接口.第一次写移动端接口就犯了一个错误,以为和web一样是怎么方便怎么来, ...

  6. NOI2009 植物大战僵尸

    啊一道好题感觉写得挺爽的啊这题这种有一点懵逼然后学了一点东西之后很明朗的感觉真是好!预处理参考 :http://www.cppblog.com/MatoNo1/archive/2014/11/01/1 ...

  7. Appsacn 定期自动化扫描

    appscan提供了计划扫描的选项,配合windows的计划任务,可以按需设定. 操作流程如下: 1.打开Appsacn--工具---扫描调度程序---新建 2.新建后显示如下窗口 3.填写好相应的设 ...

  8. windows下查看端口占用情况及关闭相应的进程

    经常,我们在启动应用的时候发现系统需要的端口被别的程序占用,如何知道谁占有了我们需要的端口,很多人都比较头疼,下面就介绍一种非常简单的方法. 例如:需要查看9001端口被谁占用,并将其进程强制关闭 在 ...

  9. 来一轮带注释的demo,彻底搞懂javascript中的replace函数

    javascript这门语言一直就像一位带着面纱的美女,总是看不清,摸不透,一直专注服务器端,也从来没有特别重视过,直到最近几年,javascript越来越重要,越来越通用.最近和前端走的比较近,借此 ...

  10. c3p0使用记录

    首先要导入c3p0包.c3p0下载解压后,lib目录下有三个包,使用mysql的话,只需要导入c3p0-0.9.5.2.jar,mchange-commons-java-0.2.11.jar. 要连接 ...