HDOJ 题目4010 Query on The Trees(Link Cut Tree连接,删边,路径点权加,路径点权最大值)

Query on The Trees

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)

Total Submission(s): 3602    Accepted Submission(s): 1587

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.

Source

The 36th ACM/ICPC Asia Regional Dalian
Site —— Online Contest

Recommend

lcy   |   We have carefully selected several similar problems for you:  4004 4005 4007 4001 4008

题目大意:n个点,n-1条边,每个点都有一个点权,操作1 连接 x y,操作2断开x y,操作3 (先输入的val)x到y的路径上的点权都加val,操作4,求x到y的路径上的最大点权值

ac代码

确实kuangbin的代码要快些,跑了800多,但是我就是看不太习惯,,还是用自己习惯的写法吧。。。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#define INF 0x7fffffff
#define N 300030
#define max(a,b) (a>b?a:b)
using namespace std;
int vis[N];
struct LCT
{
    int bef[N],pre[N],next[N][2],key[N],add[N];
	int rev[N],maxn[N];
    void init()
    {
		memset(pre,0,sizeof(pre));
		memset(next,0,sizeof(next));
        rev[0]=rev[1]=0;
		add[0]=add[1]=0;
		bef[0]=bef[1]=0;
		maxn[0]=key[0]=0;
    }
	void update_add(int x,int val)
	{
		if(x)
		{
			add[x]+=val;
			key[x]+=val;
			maxn[x]+=val;
		}
	}
	void update_rev(int x)
	{
		if(!x)
			return;
		swap(next[x][0],next[x][1]);
		rev[x]^=1;
	}
	void pushup(int x)
	{
		maxn[x] = max(key[x], max(maxn[next[x][0]], maxn[next[x][1]]));
	}
    void pushdown(int x)
    {
        if(add[x])
		{
			update_add(next[x][0],add[x]);
			update_add(next[x][1],add[x]);
			add[x]=0;
		}
		if(rev[x])
		{
			update_rev(next[x][0]);
			update_rev(next[x][1]);
		//	swap(next[x][0],next[x][1]);
			rev[x]=0;
		}
    }
    void rotate(int x,int kind)
    {
        int y,z;
        y=pre[x];
        z=pre[y];
        pushdown(y);
        pushdown(x);
        next[y][!kind]=next[x][kind];
        pre[next[x][kind]]=y;
        next[z][next[z][1]==y]=x;
        pre[x]=z;
        next[x][kind]=y;
        pre[y]=x;
        pushup(y);
    }
    void splay(int x)
    {
        int rt;
        for(rt=x;pre[rt];rt=pre[rt]);
        if(x!=rt)
        {
            bef[x]=bef[rt];
            bef[rt]=0;
			pushdown(x);
            while(pre[x])
            {
                if(next[pre[x]][0]==x)
                {
                    rotate(x,1);
                }
                else
                    rotate(x,0);
            }
			pushup(x);
        }
    }
    void access(int x)
    {
        int fa;
        for(fa=0;x;x=bef[x])
        {
            splay(x);
			pushdown(x);
            pre[next[x][1]]=0;
            bef[next[x][1]]=x;
            next[x][1]=fa;
            pre[fa]=x;
            bef[fa]=0;
            fa=x;
			pushup(x);
        }
    }
	int getroot(int x)
	{
		access(x);
		splay(x);
		while(next[x][0])
			x=next[x][0];
		return x;
	}
	void makeroot(int x)
	{
		access(x);
		splay(x);
		update_rev(x);
	}
	void link(int x,int y)
	{
		makeroot(x);
		makeroot(y);
		bef[x]=y;
	}
	void cut(int y,int x)
	{
		makeroot(y);
		access(x);
		splay(x);
		bef[next[x][0]]=bef[x];
		bef[x]=0;
		pre[next[x][0]]=0;
		next[x][0]=0;
		pushup(x);
	}
    void change(int x,int y,int val)
    {
       access(y);
        for(y=0;x;x=bef[x])
        {
            splay(x);
            if(!bef[x])
            {
                 key[x]+=val;
                 update_add(y,val);
				 update_add(next[x][1],val);
                 return;
            }
			pushdown(x);
            pre[next[x][1]]=0;
            bef[next[x][1]]=x;
            next[x][1]=y;
            pre[y]=x;
            bef[y]=0;
            y=x;
			pushup(x);
        }
    }
    int query(int x,int y)
    {
       access(y);
        for(y=0;x;x=bef[x])
        {
            splay(x);
            if(!bef[x])
            {
                 return max(key[x],max(maxn[next[x][1]],maxn[y]));
            }
			pushdown(x);
            pre[next[x][1]]=0;
            bef[next[x][1]]=x;
            next[x][1]=y;
            pre[y]=x;
            bef[y]=0;
            y=x;
			pushup(x);
        }
    }
}lct;
struct s
{
    int u,v,next;
}edge[N<<1];
int head[N],cnt;
void add(int u,int v)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void bfs(int u)
{
    queue<int>q;
    memset(vis,0,sizeof(vis));
    vis[u]=1;
    q.push(u);
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(!vis[v])
            {
                lct.bef[v]=u;
                vis[v]=1;
                q.push(v);
            }
        }
    }
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		int i;
		cnt=0;
		memset(head,-1,sizeof(head));
		for(i=1;i<n;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			add(u,v);
			add(v,u);
		}
		lct.init();
		for(i=1;i<=n;i++)
		{
			scanf("%d",&lct.key[i]);
		}
		bfs(1);
		int q;
		scanf("%d",&q);
		while(q--)
		{
			int op,x,y;
			scanf("%d%d%d",&op,&x,&y);
			if(op==1)
			{
				if(lct.getroot(x)==lct.getroot(y))
				{
					printf("-1\n");
				}
				else
					lct.link(x,y);
			}
			else
				if(op==2)
				{
					if(x==y||lct.getroot(x)!=lct.getroot(y))
					{
						printf("-1\n");
					}
					else
						lct.cut(x,y);
				}
				else
					if(op==3)
					{
						int z;
						scanf("%d",&z);
						if(lct.getroot(y)!=lct.getroot(z))
						{
							printf("-1\n");
						}
						else
							lct.change(y,z,x);
					}
					else
					{
						if(lct.getroot(x)!=lct.getroot(y))
						{
							printf("-1\n");
						}
						else
							printf("%d\n",lct.query(x,y));
					}
		}
		printf("\n");
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-26 01:10:38

HDOJ 题目4010 Query on The Trees(Link Cut Tree连接,删边,路径点权加,路径点权最大值)的相关文章

HDOJ 题目3966 Aragorn&#39;s Story(Link Cut Tree成段加减点权,查询点权)

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5505    Accepted Submission(s): 1441 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

URAL 题目1553. Caves and Tunnels(Link Cut Tree 改动点权,求两点之间最大)

1553. Caves and Tunnels Time limit: 3.0 second Memory limit: 64 MB After landing on Mars surface, scientists found a strange system of caves connected by tunnels. So they began to research it using remote controlled robots. It was found out that ther

URAL 题目1553. Caves and Tunnels(Link Cut Tree 修改点权,求两点之间最大)

1553. Caves and Tunnels Time limit: 3.0 second Memory limit: 64 MB After landing on Mars surface, scientists found a strange system of caves connected by tunnels. So they began to research it using remote controlled robots. It was found out that ther

Link Cut Tree学习笔记

从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子树信息 小结 动态树问题和Link Cut Tree 动态树问题是一类要求维护一个有根树森林,支持对树的分割, 合并等操作的问题. Link Cut Tree(林可砍树?简称LCT)是解决这一类问题的一种数据结构. 一些无聊的定义 Link Cut Tree维护的是动态森林中每棵树的任意链剖分. P

脑洞大开加偏执人格——可持久化treap版的Link Cut Tree

一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现还是可以写的,只需要实时交换答案二元组里的两棵树,最后在吧提出来的访问节点放回去就行了.本着只学一种平衡树的想法,脑洞大开加偏执人格的开始写可持久化Treap版的Link Cut Tree... 写了才发现,常数硕大啊!!!代码超长啊!!!因为merge是从上到下,split从下到上,pushdow

Codeforces Round #339 (Div. 2) A. Link/Cut Tree

A. Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure. Unfortunately, Rostislav is unable to understand the definition

LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的. 1:后接两个整数(x,y),代表连接x到y,若x到y已经联通则无需连接. 2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在. 3:后接两个整数(x,y),代表将点x上的权值变成y. 输入输出

P3690 【模板】Link Cut Tree (动态树)

P3690 [模板]Link Cut Tree (动态树) https://www.luogu.org/problemnew/show/P3690 分析: LCT模板 代码: 注意一下cut! 1 #include<cstdio> 2 #include<algorithm> 3 4 using namespace std; 5 6 const int N = 300100; 7 8 int val[N],fa[N],ch[N][2],rev[N],sum[N],st[N],top;

[模板]Link Cut Tree

传送门 Description 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的. 1:后接两个整数(x,y),代表连接x到y,若x到y已经联通则无需连接. 2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在. 3:后接两个整数(x,y),代表将点x上的权值变成y. Solution \(Link\ Cut\ Tree\)模板题