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. Redis初探(windows/linux安装)

    最近在学习Redis,先看看简介: Redis 是完全开源免费的,遵守BSD协议(可以自由的使用,修改源代码的协议,当然需要满足一定的条件),是一个高性能的key-value数据库. 特点&& ...

  2. 4.three.js中的坐标系

    Three.js中的坐标系 three.js中坐标系使用的是左手坐标系 左手坐标系和右手坐标系的对比: 当然three.js中使用的是右手坐标系 three.js中的旋转的定义 但是three.js中 ...

  3. WinDbg使用学习

    拿到软件崩溃之后产生的crash文件,后缀名为dump 使用winDbg的File-----> Open Crash Dump 打开Crash文件 File---------> Symbo ...

  4. Node.js文档和教程

    七天学会NodeJS:https://nqdeng.github.io/7-days-nodejs/ Node入门:http://www.nodebeginner.org/index-zh-cn.ht ...

  5. IntelliJ 10.0.1设置系统的JDK

    IntelliJ 10.0.1设置JDK: File-->Project Structure: project SDK--New a jsdk.

  6. MVC 漫长之路(一)

    1.新建项目 mvc  视图引擎选中 Razor   2.允许我们设置这个项目关于 MVC 的一些设置,确认选中了“空”项目模板   3.   4.打开 Global.asax 文件 配置路由名称等 ...

  7. 王者荣耀交流协会PSP Daily项目Postmortem结果

    王者荣耀交流协会PSP Daily项目Postmortem结果 整理:王超 设想和目标 1.       我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? PSP D ...

  8. Python写一个根据日期计算是星期几的模块

    import datetimedef get_week_day(date): week_day = { 0: '星期一', 1: '星期二', 2: '星期三', 3: '星期四', 4: '星期五' ...

  9. POJ 2441 Arrange the Bulls 状压dp

    题目链接: http://poj.org/problem?id=2441 Arrange the Bulls Time Limit: 4000MSMemory Limit: 65536K 问题描述 F ...

  10. week2:个人博客作业

    1.这周写程序感想: 昨天差不多,也是这个时候看些的程序,写了4个小时程序,感触颇多.昨日,原打算,就完成程序就吧这个随笔写完结果,由于各种原因,没写一直拖到现在.就做昨天写的程序,本身题目很简单,主 ...