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 ...
随机推荐
- Spring Security登录验证流程源码解析
一.登录认证基于过滤器链 Spring Security的登录验证流程核心就是过滤器链.当一个请求到达时按照过滤器链的顺序依次进行处理,通过所有过滤器链的验证,就可以访问API接口了. SpringS ...
- 前端与算法 leetcode 7. 整数反转
目录 # 前端与算法 leetcode 7. 整数反转 题目描述 概要 提示 解析 解法 算法 传入测试用例的运行结果 执行结果 GitHub仓库 # 前端与算法 leetcode 7. 整数反转 题 ...
- Django 通过 ORM 实现表的CRUD
Django 通过 ORM 实现表的CRUD 单表的创建 修改 setting.py 文件 DATABASES = { 'default': { 'ENGINE': 'django.db.backen ...
- linux后台运行程序--nobup
用途:不挂断地运行命令. 语法:nohup Command [ Arg - ] [ & ] 描述:nohup 命令运行由 Command 参数和任何相关的 Arg 参数指定的命令,忽略所有挂断 ...
- ubuntu 16.04 上使用pybind11进行C++和Python代码相互调用 | Interfacing C++ and Python with pybind11 on ubuntu 16.04
本文首发于个人博客https://kezunlin.me/post/a41adc1/,欢迎阅读! Interfacing C++ and Python with pybind11 on ubuntu ...
- MySQL锁会不会,你就差看一看
数据库锁知识 不少人在开发的时候,应该很少会注意到这些锁的问题,也很少会给程序加锁(除了库存这些对数量准确性要求极高的情况下),即使我们不会这些锁知识,我们的程序在一般情况下还是可以跑得好好的.因为这 ...
- 20191019-3 alpha week 2/2 Scrum立会报告+燃尽图 03
此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/9799 一.小组情况 队名:扛把子 组长:迟俊文 组员:宋晓丽 梁梦瑶 韩昊 ...
- 2019-9-11:渗透测试,基础学习,ubuntu搭建LAMP
一,apache web服务器安装 1,sudo apt-get install apache2 2,systemctl status apache2,检查apache2是否开启 #开启.关闭和重启a ...
- day03_正则表达式
1.数据分类 数据的分类 定义:数据以行为单位,每一个数据表示一个实体的信息.每一行数据的属性都是一样的. 常见的结构化数据为关系型数据库存储数据. 半结构化数据 定义:结构化数据的另一种 ...
- 【Python3爬虫】网络小说更好看?十四万条书籍信息告诉你
一.前言简述 因为最近微信读书出了网页版,加上自己也在闲暇的时候看了两本书,不禁好奇什么样的书更受欢迎,哪位作者又更受读者喜欢呢?话不多说,爬一下就能有个了解了. 二.页面分析 首先打开微信读书:ht ...