【hdu4010】 Query on The Trees
http://acm.hdu.edu.cn/showproblem.php?pid=4010 (题目链接)
题意
link cut tree板子
Solution
link cut tree
细节
注意第二个询问切的是什么
代码
// hdu4010
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#define LL long long
#define inf 2147483647
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=300010;
int val[maxn],tag[maxn],rev[maxn],fa[maxn],mx[maxn],tr[maxn][2];
int n,m; void Init() {
memset(tag,0,sizeof(tag));memset(fa,0,sizeof(fa));
memset(rev,0,sizeof(rev));memset(tr,0,sizeof(tr));
mx[0]=-inf;
}
void pushup(int x) {
mx[x]=max(mx[tr[x][0]],mx[tr[x][1]]);
mx[x]=max(mx[x],val[x]);
}
void pushdown(int x) {
int l=tr[x][0],r=tr[x][1];
if (tr[fa[x]][0]==x || tr[fa[x]][1]==x) pushdown(fa[x]);
if (rev[x]) {
swap(tr[x][0],tr[x][1]);
rev[l]^=1;rev[r]^=1;rev[x]^=1;
}
if (tag[x]) {
if (l) tag[l]+=tag[x],mx[l]+=tag[x],val[l]+=tag[x];
if (r) tag[r]+=tag[x],mx[r]+=tag[x],val[r]+=tag[x];
tag[x]=0;
}
}
void rotate(int x) {
int y=fa[x],z=fa[y],l,r;
if (tr[y][0]==x) l=0;else l=1;r=l^1;
if (tr[fa[y]][0]==y || tr[fa[y]][1]==y) tr[z][tr[z][1]==y]=x;
fa[x]=z;fa[y]=x;fa[tr[x][r]]=y;
tr[y][l]=tr[x][r];tr[x][r]=y;
pushup(y);pushup(x);
}
void splay(int x) {
pushdown(x);
while (tr[fa[x]][0]==x || tr[fa[x]][1]==x) {
int y=fa[x],z=fa[y];
if (tr[z][0]==y || tr[z][1]==y) {
if (tr[y][0]==x ^ tr[z][0]==y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
int find(int x) {
while (fa[x]) x=fa[x];
return x;
}
void access(int x) {
for (int y=0;x;y=x,x=fa[x])
splay(x),tr[x][1]=y,pushup(x);
}
void makeroot(int x) {
access(x);splay(x);rev[x]^=1;
}
void link(int x,int y) {
makeroot(x);fa[x]=y;
}
void cut(int x,int y) {
makeroot(x);access(y);splay(y);
tr[y][0]=fa[tr[y][0]]=0;//注意这个地方,要求断的是y和其祖先的边
pushup(y);
}
void modify(int x,int y,int w) {
makeroot(x);access(y);splay(y);
tag[y]+=w;mx[y]+=w;val[y]+=w;
}
int query(int x,int y) {
makeroot(x);access(y);splay(y);
return mx[y];
}
int main() {
while (scanf("%d",&n)!=EOF) {
Init();
for (int u,v,i=1;i<n;i++) {
scanf("%d%d",&u,&v);
link(u,v);
}
for (int i=1;i<=n;i++) scanf("%d",&val[i]),mx[i]=val[i];
scanf("%d",&m);
for (int op,x,y,w,i=1;i<=m;i++) {
scanf("%d",&op);
if (op==1) {
scanf("%d%d",&x,&y);
if (find(x)==find(y)) {puts("-1");continue;}
link(x,y);
}
if (op==2) {
scanf("%d%d",&x,&y);
if (x==y || find(x)!=find(y)) {puts("-1");continue;}
cut(x,y);
}
if (op==3) {
scanf("%d%d%d",&w,&x,&y);
if (find(x)!=find(y)) {puts("-1");continue;}
modify(x,y,w);
}
if (op==4) {
scanf("%d%d",&x,&y);
if (find(x)!=find(y)) {puts("-1");continue;}
printf("%d\n",query(x,y));
}
}
puts("");
}
return 0;
}
【hdu4010】 Query on The Trees的更多相关文章
- 【HDU4010】【LCT】Query on The Trees
Problem Description We have met so many problems on the tree, so today we will have a query problem ...
- 【leetcode】 Unique Binary Search Trees (middle)☆
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【动态规划】Codeforces 711C Coloring Trees
题目链接: http://codeforces.com/problemset/problem/711/C 题目大意: 给N棵树,M种颜色,已经有颜色的不能涂色,没颜色为0,可以涂色,每棵树I涂成颜色J ...
- 【leetcode】Unique Binary Search Trees (#96)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【leetcode】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【leetcode】 Unique Binary Search Trees II (middle)☆
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【HDU】1693 Eat the Trees
http://acm.hdu.edu.cn/showproblem.php?pid=1693 题意:n×m的棋盘求简单回路(可以多条)覆盖整个棋盘的方案,障碍格不许摆放.(n,m<=11) #i ...
- 【题解】【BST】【Leetcode】Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- php快速上手总结
PHP作为现代热门主流的开发语言,对于那些想加入新手PHPer,从哪学起,如何学习?你必须要需要掌握PHP的基础知识,基础知识相当于重点,是不可忽视的知识.常用的功能模块,面向对象的,MVC等相关技能 ...
- 【snaptype nexus】搭建maven私服仓库
搭建本地开发私库,旨在解决本地开始受限于网络环境的问题:具体的搭建步骤主要包含以下几个步骤: 1.私服服务器使用的是ubuntu,首先下载安装包(版本号:2.12),下载地址:http://www.s ...
- Echarts服务端生成图片
Echarts是百度发布的一套优秀的浏览器端图表控件,Echarts是基于html5的cavens绘图实现.而使用server端生成图片无法借用浏览器端渲染.通用的做法有两种: 是用headless浏 ...
- Netty源码分析第3章(客户端接入流程)---->第1节: 初始化NioSockectChannelConfig
Netty源码分析第三章: 客户端接入流程 概述: 之前的章节学习了server启动以及eventLoop相关的逻辑, eventLoop轮询到客户端接入事件之后是如何处理的?这一章我们循序渐进, 带 ...
- 机器人平台框架Yarp - Yet another robot platform
简介 ROS有强大和易用的特性,用的人很多,目前已经推出2.0版本,有相关的官网和论坛.然而其缺点也比较明显. 只能基于Ubuntu系统,且一个ROS版本只能对应一个具体的Ubuntu版本 通信 ...
- golang slice使用不慎导致的问题
原文链接 : http://www.bugclosed.com/post/16 背景 go语言中切片slice是方便且好用的强大数据结构,但是使用的时候需要注意,不然容易出问题,最近因为遇到了一个sl ...
- spring-boot rabbitMq 完整项目搭建,包括创建、发送、监听
写在开始 rabbitMq 代码按照三部分介绍 第一部分 交换机和队列的创建 第二部分 消息发送 第三部分 消息监听 第一部分 1 建立queue 2 建立exchange 3 exchange绑定q ...
- PSP Daily软件beta版本——基于NABCD评论,及改进建议
1.根据(不限于)NABCD评论作品的选题: 此软件的用户人群较为明确,即:用户(软件工程课上学生)记录例行报告.写每周PSP表格和统计的需求.潜在用户还有未来该课堂的学生和需要用PSP方法记录任务完 ...
- python中数据分析常用函数整理
一. apply函数 作用:对 DataFrame 的某行/列应用函数之后,Apply 返回一些值.函数既可以使用默认的,也可以自定义.注意:在第二个输出中应用 head() 函数,因为它包含了很多行 ...
- Scrum Meeting 10.27
1.会议内容: 姓名 今日任务 明日任务 预估时间(h) 徐越 配置SQLserver 学习本地和服务器之间的通信 4 卞忠昊 找上届代码的bug 学习安卓布局(layout)的有关知识,研究上届学长 ...