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的更多相关文章

  1. 【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 ...

  2. 【leetcode】 Unique Binary Search Trees (middle)☆

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  3. 【动态规划】Codeforces 711C Coloring Trees

    题目链接: http://codeforces.com/problemset/problem/711/C 题目大意: 给N棵树,M种颜色,已经有颜色的不能涂色,没颜色为0,可以涂色,每棵树I涂成颜色J ...

  4. 【leetcode】Unique Binary Search Trees (#96)

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  5. 【leetcode】Unique Binary Search Trees

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  6. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  7. 【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 ...

  8. 【HDU】1693 Eat the Trees

    http://acm.hdu.edu.cn/showproblem.php?pid=1693 题意:n×m的棋盘求简单回路(可以多条)覆盖整个棋盘的方案,障碍格不许摆放.(n,m<=11) #i ...

  9. 【题解】【BST】【Leetcode】Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

随机推荐

  1. canvas高效绘制10万图形,你必须知道的高效绘制技巧

    最近的一个客户项目中,简化的需求是绘制按照行列绘制很多个圆圈.需求看起来不难,上手就可以做,写两个for循环. 原始绘制方法 首先定义了很多Circle对象,在遍历循环中调用该对象的draw方法.代码 ...

  2. 分析(function(window, undefined) {})(window)

    有的时候,我们会在JS框架中看到这行 (function(window, undefined) {})(window) ,它是做什么用的,我们来分析下它 首先这就是一个匿名函数,立即执行它 (func ...

  3. 如何在window服务器上搭建一个能代替ftp的传输工具

    通常对于服务器上的文件管理和数据传输都是利用ftp来实现,但随着存储技术的发展,数据资产的存储规模和复杂程度不断提高,传统的ftp传输显得有笨重.今天给大家介绍一款能够取代ftp的在线文档管理软件—— ...

  4. 基于Ubuntu+kodexplorer可道云的私有云网盘

    1.可用的服务器:组装PC机一台,操作系统为Ubuntu 14.04 LTS,无桌面环境,放在机房,使用远程终端进行访问.有安装了Apache2,运行着svn服务.内网IP地址为192.168.0.1 ...

  5. Redis源码阅读(三)集群-连接初始化

    Redis源码阅读(三)集群-连接建立 对于并发请求很高的生产环境,单个Redis满足不了性能要求,通常都会配置Redis集群来提高服务性能.3.0之后的Redis支持了集群模式. Redis官方提供 ...

  6. java并发编程原理

    一.java内存模型 Java内存模型的主要目标是定义程序中各个变量的访问规则,即在虚拟机中将变量存储到内存和从内存中取出变量这样底层细节.此处的变量与Java编程时所说的变量不一样,指包括了实例字段 ...

  7. wf4 FlowChart 理解

    1. 工作流是异步的,所以需要信号. 2. 需要创建 NativeActivity<T> 与整个FlowChart交互. 它的返回值 Result 正确含义是 该 Activity处理之后 ...

  8. 卸载CentOS7自带的OpenJDK

    http://blog.csdn.net/xiegh2014/article/details/52343438

  9. OO第三次阶段总结

    (1)调研,规格化设计的大致发展和为什么得到人类重视 结构化程序设计(英语:Structured programming),一种编程范型.它采用子程序(函数就是一种子程序).代码区块.for循环以及w ...

  10. C++:类中的赋值函数

    先来看一个例子: #include<iostream> #include<string> using namespace std; class Student{ public: ...