HDOJ 4010 Query on The Trees(LCT动态树)

来源:互联网 发布:c语言从入门到精通光盘 编辑:程序博客网 时间:2024/05/19 13:22
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
51 22 42 51 31 2 3 4 564 2 32 1 24 2 31 3 53 2 1 44 1 4
Sample Output
3-17
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不在同一棵树上则在xy连边 2、如果x和y在同一棵树上并且x!=y则把x换为树根并把y和y的父亲分离 3、如果x和y在同一棵树上则x到y的路径上所有的点权值+w 4、如果x和y在同一棵树上则输出x到y路径上的最大值
注意:(心血。。被这几个点坑了好久)

1.有多组数据

2.第3,4个操作可以x==y

3.第3个操作中先输入w,再输入x和y;

4.每个操作不满足,输出-1

5.最后每组数据结束后必须要输出空行

#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#define F(x) tree[x].fa#define LC(x) tree[x].child[0]#define RC(x) tree[x].child[1]#define REV(x) tree[x].rev#define Size 300010using namespace std;inline int read(){int sum=0,fg=1;char c=getchar();while(c<'0' || c>'9'){if(c=='-')fg=-1;c=getchar();}while(c>='0' && c<='9'){sum=sum*10+c-'0';c=getchar();}return sum*fg;}struct lct{int fa,child[2],rev,add;int v,MAX;}tree[Size];int be[Size],ne[Size],to[Size],e;struct link_cut_tree{inline bool isroot(int x){return LC(F(x))!=x && RC(F(x))!=x;}inline void increase(int x,int val){tree[x].v+=val;tree[x].MAX+=val;tree[x].add+=val;}inline void pushup(int x){tree[x].MAX=max(tree[LC(x)].MAX,tree[RC(x)].MAX);tree[x].MAX=max(tree[x].MAX,tree[x].v);}inline void pushdown(int x){if(REV(x)){REV(x)^=1;REV(LC(x))^=1;REV(RC(x))^=1;swap(LC(x),RC(x));}if(tree[x].add){if(LC(x))increase(LC(x),tree[x].add);if(RC(x))increase(RC(x),tree[x].add);tree[x].add=0;}}void Pushdown(int x){if(!isroot(x))Pushdown(F(x));pushdown(x);}inline void rotate(int x){int A=F(x),B=F(A);bool w=(RC(A)==x);if(!isroot(A)){if(LC(B)==A)LC(B)=x;else if(RC(B)==A)RC(B)=x;}F(tree[x].child[w^1])=A;F(A)=x;F(x)=B;tree[A].child[w]=tree[x].child[w^1];tree[x].child[w^1]=A;pushup(A);pushup(x);}inline void splay(int x){Pushdown(x);while(!isroot(x)){if(!isroot(F(x)))rotate(x);rotate(x);}}inline void access(int x){for(int i=0;x;i=x,x=F(x))splay(x),RC(x)=i,pushup(x);}inline int find_root(int x){access(x);splay(x);while(LC(x))x=LC(x);return x;}inline void reverse(int x){access(x);splay(x);REV(x)^=1;}inline int link(int x,int y){if(find_root(x)==find_root(y) || x==y)return -1;reverse(x);F(x)=y;return 0;}inline int cut(int x,int y){if(find_root(x)!=find_root(y) || x==y)return -1;reverse(x);access(y);splay(y);F(LC(y))=0;LC(y)=0;pushup(y);return 0;}inline int add(int x,int y,int w){if(find_root(x)!=find_root(y))return -1;reverse(x);access(y);splay(y);increase(y,w);return 0;}inline int query(int x,int y){if(find_root(x)!=find_root(y))return -1;reverse(x);access(y);splay(y);return tree[y].MAX;}}LCT;void add(int x,int y){to[++e]=y;ne[e]=be[x];be[x]=e;}void dfs(int x,int fa){for(int i=be[x];i;i=ne[i]){int v=to[i];if(v!=fa){tree[v].fa=x;dfs(v,x);}}}void init(){memset(be,0,sizeof(be));e=0;memset(tree,0,sizeof(tree));}int main(){int n;while(scanf("%d",&n)!=EOF){init();int x,y;for(int i=1;i<n;i++){x=read();y=read();add(x,y);add(y,x);}dfs(1,0);for(int i=1;i<=n;i++){x=read();tree[i].v=tree[i].MAX=x;}int m;m=read();while(m--){int tp;tp=read();int x,y,w;if(tp==1){x=read();y=read();if(LCT.link(x,y)==-1)printf("-1\n");}else if(tp==2){x=read();y=read();if(LCT.cut(x,y)==-1)printf("-1\n");}else if(tp==3){w=read();x=read();y=read();if(LCT.add(x,y,w)==-1)printf("-1\n");}else{x=read();y=read();printf("%d\n",LCT.query(x,y));}}puts("");//这道题一定最后要输出空行,被坑了好久。。 }return 0;}

0 0
原创粉丝点击