HDU5002 tree
Your task is to deal with M operations of 4 types:
1.Delete an edge (x, y) from the tree, and then add a new edge (a, b). We ensure that it still constitutes a tree after adding the new edge.
2.Given two nodes a and b in the tree, change the weights of all the nodes on the path connecting node a and b (including node a and b) to a particular value x.
3.Given two nodes a and b in the tree, increase the weights of all the nodes on the path connecting node a and b (including node a and b) by a particular value d.
4.Given two nodes a and b in the tree, compute the second largest weight on the path connecting node a and b (including node a and b), and the number of times this weight occurs on the path. Note that here we need the strict second largest weight. For instance, the strict second largest weight of {3, 5, 2, 5, 3} is 3.
InputThe first line contains an integer T (T<=3), which means there are T test cases in the input.
For each test case, the first line contains two integers N and M (N, M<=10^5). The second line contains N integers, and the i-th integer is the weight of the i-th node in the tree (their absolute values are not larger than 10^4).
In next N-1 lines, there are two integers a and b (1<=a, b<=N), which means there exists an edge connecting node a and b.
The next M lines describe the operations you have to deal with. In each line the first integer is c (1<=c<=4), which indicates the type of operation.
If c = 1, there are four integers x, y, a, b (1<= x, y, a, b <=N) after c.
If c = 2, there are three integers a, b, x (1<= a, b<=N, |x|<=10^4) after c.
If c = 3, there are three integers a, b, d (1<= a, b<=N, |d|<=10^4) after c.
If c = 4 (it is a query operation), there are two integers a, b (1<= a, b<=N) after c.
All these parameters have the same meaning as described in problem description.OutputFor each test case, first output "Case #x:"" (x means case ID) in a separate line.
For each query operation, output two values: the second largest weight and the number of times it occurs. If the weights of nodes on that path are all the same, just output "ALL SAME" (without quotes).Sample Input
2
3 2
1 1 2
1 2
1 3
4 1 2
4 2 3
7 7
5 3 2 1 7 3 6
1 2
1 3
3 4
3 5
4 6
4 7
4 2 6
3 4 5 -1
4 5 7
1 3 4 2 4
4 3 6
2 3 6 5
4 3 6
Sample Output
Case #1:
ALL SAME
1 2
Case #2:
3 2
1 1
3 2
ALL SAME 题解:
else if(val==mx1[x])c1[x]+=c;
else if(val>mx2[x])mx2[x]=val,c2[x]=c;
else if(val==mx2[x])c2[x]+=c;
solve(x,mx1[l],c1[l]),solve(x,mx2[l],c2[l]);
solve(x,mx1[r],c1[r]),solve(x,mx2[r],c2[r]);
参考代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#define inf 2000000000
#define ll long long
#define N 100005
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int T;
int n,m,top;
int q[N];
int c[N][],fa[N],v[N];
int mx1[N],mx2[N],c1[N],c2[N],size[N];
int ta[N],tc[N];
bool rev[N];
void solve(int x,int val,int c)
{
if(val>mx1[x])mx2[x]=mx1[x],mx1[x]=val,c2[x]=c1[x],c1[x]=c;
else if(val==mx1[x])c1[x]+=c;
else if(val>mx2[x])mx2[x]=val,c2[x]=c;
else if(val==mx2[x])c2[x]+=c;
}
void update(int x)
{
int l=c[x][],r=c[x][];
mx1[x]=mx2[x]=-inf;c1[x]=c2[x]=;
solve(x,v[x],);
if(l)solve(x,mx1[l],c1[l]),solve(x,mx2[l],c2[l]);
if(r)solve(x,mx1[r],c1[r]),solve(x,mx2[r],c2[r]);
size[x]=size[l]+size[r]+;
}
void add(int y,int val)
{
mx1[y]+=val;v[y]+=val;
if(mx2[y]!=-inf)mx2[y]+=val;
ta[y]+=val;
}
void change(int y,int val)
{
mx1[y]=val;v[y]=val;c1[y]=size[y];
mx2[y]=-inf;c2[y]=;
tc[y]=val;
if(ta[y])ta[y]=;
}
void pushdown(int x)
{
int l=c[x][],r=c[x][];
if(rev[x])
{
rev[x]^=;rev[l]^=;rev[r]^=;
swap(c[x][],c[x][]);
}
if(tc[x]!=-inf)
{
if(l)change(l,tc[x]);
if(r)change(r,tc[x]);
tc[x]=-inf;
}
if(ta[x])
{
if(l)add(l,ta[x]);
if(r)add(r,ta[x]);
ta[x]=;
}
}
bool isroot(int x)
{
return c[fa[x]][]!=x&&c[fa[x]][]!=x;
}
void rotate(int x)
{
int y=fa[x],z=fa[y],l,r;
if(c[y][]==x)l=;else l=;r=l^;
if(!isroot(y))
{
if(c[z][]==y)c[z][]=x;else c[z][]=x;
}
fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;
update(y);update(x);
}
void splay(int x)
{
top=;q[++top]=x;
for(int i=x;!isroot(i);i=fa[i])
q[++top]=fa[i];
while(top)pushdown(q[top--]);
while(!isroot(x))
{
int y=fa[x],z=fa[y];
if(!isroot(y))
{
if(c[y][]==x^c[z][]==y)rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
for(int t=;x;t=x,x=fa[x])
splay(x),c[x][]=t,update(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);
c[y][]=fa[x]=;update(y);
}
void query(int x,int y)
{
makeroot(x);access(y);splay(y);
if(c1[y]==size[y]) puts("ALL SAME");
else printf("%d %d\n",mx2[y],c2[y]);
}
int main()
{
T=read();
for(int cas=;cas<=T;cas++)
{
printf("Case #%d:\n",cas);
n=read();m=read();
for(int i=;i<=n;i++)
v[i]=read();
for(int i=;i<=n;i++)
{
mx1[i]=v[i],c1[i]=;
mx2[i]=-inf,c2[i]=;
size[i]=;
}
for(int i=;i<=n;i++)
{
fa[i]=c[i][]=c[i][]=;
ta[i]=rev[i]=;tc[i]=-inf;
}
for(int i=;i<n;i++)
{
int u=read(),v=read();
link(u,v);
}
int opt,x,y,a,b,d;
while(m--)
{
opt=read();
if(opt==)
{
x=read();y=read();a=read();b=read();
cut(x,y);link(a,b);
}
else if(opt==)
{
a=read();b=read();x=read();
makeroot(a);access(b);splay(b);
change(b,x);
}
else if(opt==)
{
a=read();b=read();d=read();
makeroot(a);access(b);splay(b);
add(b,d);
}
else
{
a=read();b=read();
query(a,b);
}
}
}
return ;
}
HDU5002 tree的更多相关文章
- HDU5002 Tree(LCT)
今天做了一道LCT模板题之后忽然间好像记起来LCT的模板怎么用了,于是就把上次网络赛的一道LCT补一下.典型的删边,加边操作,还有路径加和路径set为一个数.维护的是路径第二大以及它有多少个,后来想想 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- python中字典的基础操作
dict1 = { 'name':'王麻子', 'age':25, 'phone':12580, 'high':160 } dict2 = { 'name':'张三', 'age':38, 'phon ...
- PowerDesigner列名、注释内容互换
资料来源:PowerDesigner列名.注释内容互换 文中一共提供了2种操作的代码. (1)将Name中的字符COPY至Comment中 (2)将Comment中的字符COPY至Name中 使用方法 ...
- C++中对C的扩展学习新增语法——动态内存管理
1.C语言动态内存管理的缺点: 1.malloc对象的大小需要自己计算. 2.需要手动转换指针类型. 3.C++的对象不适合使用malloc和free. 2.C++中new/delete基本使用: 3 ...
- mysql中int、bigint、smallint、tinyint 长度
mysql中int.bigint.smallint.tinyint 长度 bigint -2^63 (-9223372036854775808) 到 2^63-1 (92233720368547758 ...
- hdu 1083 Courses (最大匹配)
CoursesTime Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- Apache Hudi 介绍与应用
Apache Hudi Apache Hudi 在基于 HDFS/S3 数据存储之上,提供了两种流原语: 插入更新 增量拉取 一般来说,我们会将大量数据存储到HDFS/S3,新数据增量写入,而旧数据鲜 ...
- FB力挺的Pytorch深度学习 书本来了
获得 fb首席科学家力挺的 pytorch教程 发布啦, 看截图 ![file](https://img2018.cnblogs.com/blog/1876748/201911/1876748-201 ...
- tar使用笔记
解压到指定目录 -C tar -zxvf test.tar.gz -C /opt
- (线段树)A Corrupt Mayor's Performance Art
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 题意: 区间更新, 区间询问: 题解: 区间更新, 区间询问, 一共30种颜色, 可用int 来 ...
- windows 10上源码编译dlib教程 | compile dlib on windows 10
本文首发于个人博客https://kezunlin.me/post/654a6d04/,欢迎阅读! compile dlib on windows 10 Series Part 1: compile ...