题目1:BZOJ 2049 洞穴勘测

 #include <bits/stdc++.h>
#define L(x) c[x][0]
#define R(x) c[x][1] using namespace std; const int oo = 0x3f3f3f3f; struct SplayTree{
static const int N = + ; int top, st[N];
int fa[N], c[N][], sum[N], mx[N], val[N];
bool rev[N]; bool isroot(int x){
return L(fa[x]) != x && R(fa[x]) != x;
} void pushdown(int x){
int l = L(x), r = R(x); if(rev[x]){
rev[x] ^= ; rev[l] ^= ; rev[r] ^= ;
swap(L(x), R(x));
}
} void pushup(int x){
int l = L(x), r = R(x); sum[x] = sum[l] + sum[r] + val[x];
mx[x] = max(val[x], max(mx[l], mx[r]));
} void rotate(int x){
int y = fa[x], z = fa[y], l, r; l = (L(y) == x) ^ ; r = l ^ ;
if(!isroot(y))
c[z][(L(z) == y) ^ ] = x;
fa[x] = z; fa[y] = x; fa[c[x][r]] = y;
c[y][l] = c[x][r]; c[x][r] = y;
} void splay(int x){
top = ;
st[++ top] = x;
for(int i = x; !isroot(i); i = fa[i])
st[++ top] = fa[i];
while(top) pushdown(st[top --]);
while(!isroot(x)){
int y = fa[x], z = fa[y]; if(!isroot(y)){
if(L(y) == x ^ L(z) == y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
}Splay; struct LinkCutTree{ void Access(int x){
int t = ; while(x){
Splay.splay(x);
Splay.R(x) = t;
t = x; x = Splay.fa[x];
}
} void makeroot(int x){
Access(x); Splay.splay(x); Splay.rev[x] ^= ;
} void link(int x, int y){
makeroot(x); Splay.fa[x] = y; Splay.splay(x);
} void cut(int x, int y){
makeroot(x); Access(y); Splay.splay(y);
Splay.L(y) = Splay.fa[x] = ;
} int findroot(int x){
Access(x); Splay.splay(x);
int y = x;
while(Splay.L(y)) y = Splay.L(y); return y;
}
}lct; int n, m, x, y;
char str[]; int main(){
scanf("%d%d", &n, &m);
for(int i = ; i <= m; ++ i){
scanf("%s%d%d", str, &x, &y); if(str[] == 'C') lct.link(x, y);
else if(str[] == 'D') lct.cut(x, y);
else{
if(lct.findroot(x) == lct.findroot(y)) printf("Yes\n");
else printf("No\n");
}
} return ;
}

BZOJ 2049

题目2: BZOJ 1180 OTOCI

 #include <bits/stdc++.h>
#define L(x) c[x][0]
#define R(x) c[x][1] using namespace std; typedef long long ll;
const int N = + ;
const int oo = 0x3f3f3f3f; struct SplayTree{
int fa[N], c[N][];
ll val[N], mx[N], sum[N];
bool rev[N];
int top, st[N]; inline bool isroot(int x){
return L(fa[x]) != x && R(fa[x]) != x;
} void pushdown(int x){
int l = L(x), r = R(x); if(rev[x]){
rev[x] ^= ; rev[l] ^= ; rev[r] ^= ;
swap(L(x), R(x));
}
} void pushup(int x){
int l = L(x), r = R(x); sum[x] = sum[l] + sum[r] + val[x];
mx[x] = max(val[x], max(mx[l], mx[r]));
} void rotate(int x){
int y = fa[x], z = fa[y], l, r; l = (L(y) == x) ^ ; r = l ^ ;
if(!isroot(y)){
c[z][(L(z) == y) ^ ] = x;
}
fa[x] = z; fa[y] = x; fa[c[x][r]] = y;
c[y][l] = c[x][r]; c[x][r] = y;
pushup(y); pushup(x);
} void splay(int x){
top = ;
st[++ top] = x; for(int i = x; !isroot(i); i = fa[i])
st[++ top] = fa[i];
while(top) pushdown(st[top --]);
while(!isroot(x)){
int y = fa[x], z = fa[y]; if(!isroot(y)){
if(L(y) == x ^ L(z) == y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
}Splay; struct LinkCutTree{
void Access(int x){
int t = ; while(x){
Splay.splay(x);
Splay.R(x) = t;
t = x;
Splay.pushup(x);
x = Splay.fa[x];
}
} void makeroot(int x){
Access(x); Splay.splay(x); Splay.rev[x] ^= ;
} void link(int x, int y){
makeroot(x); Splay.fa[x] = y; Splay.splay(x);
} void cut(int x, int y){
makeroot(x); Access(y); Splay.splay(y);
Splay.L(y) = Splay.fa[x] = ;
} int findroot(int x){
Access(x); Splay.splay(x); int y = x; while(Splay.L(y)){
y = Splay.L(y);
} return y;
}
}lct; int n, m, x, y;
char str[]; int main(){
scanf("%d", &n);
for(int i = ; i <= n; ++ i){
scanf("%lld", &Splay.val[i]);
Splay.sum[i] = Splay.val[i];
}
scanf("%d", &m);
for(int i = ; i <= m; ++ i){
scanf("%s%d%d", str, &x, &y); if(str[] == 'b'){
if(lct.findroot(x) == lct.findroot(y))
puts("no");
else{
puts("yes"); lct.link(x, y);
}
}
else if(str[] == 'e'){
if(lct.findroot(x) != lct.findroot(y)){
puts("impossible");
}
else{
lct.makeroot(x); lct.Access(y); Splay.splay(y);
printf("%lld\n", Splay.sum[y]);
}
}
else{
lct.makeroot(x); Splay.val[x] = y; Splay.pushup(x);
}
} return ;
}

BZOJ 1180

题目3:BZOJ 3282 TREE

 #include <bits/stdc++.h>
#define L(x) c[x][0]
#define R(x) c[x][1] using namespace std; const int N = + ; struct SplayTree{
int fa[N], c[N][], val[N], sum[N];
bool rev[N];
int top, st[N]; inline bool isroot(int x){
return L(fa[x]) != x && R(fa[x]) != x;
} void pushup(int x){
int l = L(x), r = R(x); sum[x] = sum[l] ^ sum[r] ^ val[x];
}
void pushdown(int x){
int l = L(x), r = R(x); if(rev[x]){
rev[x] ^= ; rev[l] ^= ; rev[r] ^= ;
swap(L(x), R(x));
}
}
void rotate(int x){
int y = fa[x], z = fa[y], l, r; l = (L(y) == x) ^ ; r = l ^ ; if(!isroot(y)){
c[z][(L(z) == y) ^ ] = x;
}
fa[y] = x; fa[x] = z; fa[c[x][r]] = y;
c[y][l] = c[x][r]; c[x][r] = y;
pushup(y); pushup(x);
} void splay(int x){
top = ;
st[++ top] = x;
for(int i = x; !isroot(i); i = fa[i])
st[++ top] = fa[i]; while(top) pushdown(st[top --]);
while(!isroot(x)){
int y = fa[x], z = fa[y]; if(!isroot(y)){
if(L(y) == x ^ L(z) == y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
}Splay; struct LinkCutTree{
void Access(int x){
int t = ; while(x){
Splay.splay(x);
Splay.R(x) = t;
t = x;
Splay.pushup(x);
x = Splay.fa[x];
}
} void makeroot(int x){
Access(x); Splay.splay(x); Splay.rev[x] ^= ;
} int findroot(int x){
Access(x); Splay.splay(x); int y = x; while(Splay.L(y)) y = Splay.L(y); return y;
} void link(int x, int y){
makeroot(x); Splay.fa[x] = y; Splay.splay(x);
} void cut(int x, int y){
makeroot(x); Access(y); Splay.splay(y);
Splay.L(y) = Splay.fa[x] = ;
}
}lct; int n, m;
int x, y, z; int main(){
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++ i){
scanf("%d", &Splay.val[i]);
Splay.sum[i] = Splay.val[i];
}
for(int i = ; i <= m; ++ i){
scanf("%d%d%d", &z, &x, &y); switch(z){
case :{
lct.makeroot(x); lct.Access(y); Splay.splay(y);
printf("%d\n", Splay.sum[y]);
break;
}
case :{
if(lct.findroot(x) != lct.findroot(y)){
lct.link(x, y);
}
break;
}
case :{
if(lct.findroot(x) == lct.findroot(y)){
lct.cut(x, y);
}
break;
}
case :{
lct.Access(x); Splay.splay(x); Splay.val[x] = y;
Splay.pushup(x);
break;
}
}
} return ;
}

BZOJ 3282

题目4: HDU 4010 Query On The Trees

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#define L(x) c[x][0]
#define R(x) c[x][1] using namespace std; const int N = + ;
const int oo = 0x3f3f3f3f; int n, m, tot;
int first[N], nxt[N<<];
int u[N<<], v[N<<];
int que[N<<]; struct SplayTree{
int fa[N], c[N][], val[N], mx[N], add[N];
bool rev[N];
int top, st[N]; inline bool isroot(int x){
return L(fa[x]) != x && R(fa[x]) != x;
} void pushup(int x){
int l = L(x), r = R(x); mx[x] = max(mx[l], mx[r]);
mx[x] = max(mx[x], val[x]);
} void pushdown(int x){
int l = L(x), r = R(x); if(rev[x]){
rev[x] ^= ; rev[l] ^= ; rev[r] ^= ;
swap(L(x), R(x));
}
if(add[x]){
if(l){
add[l] += add[x]; mx[l] += add[x]; val[l] += add[x];
}
if(r){
add[r] += add[x]; mx[r] += add[x]; val[r] += add[x];
}
add[x] = ;
}
} void rotate(int x){
int y = fa[x], z = fa[y], l, r; l = (L(y) == x) ^ ; r = l ^ ;
if(!isroot(y)){
c[z][L(z) == y ^ ] = x;
}
fa[x] = z; fa[y] = x; fa[c[x][r]] = y;
c[y][l] = c[x][r]; c[x][r] = y;
pushup(y); pushup(x);
} void splay(int x){
top = ;
st[++ top] = x;
for(int i = x; !isroot(i); i = fa[i])
st[++ top] = fa[i];
while(top) pushdown(st[top --]);
while(!isroot(x)){
int y = fa[x], z = fa[y]; if(!isroot(y)){
if(L(y) == x ^ L(z) == y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
}Splay; struct LinkCutTree{
void Access(int x){
int t = ; while(x){
Splay.splay(x);
Splay.R(x) = t;
t = x;
Splay.pushup(x);
x = Splay.fa[x];
}
}
void Add(int x, int y, int w){
makeroot(x); Access(y); Splay.splay(y);
Splay.add[y] += w; Splay.val[y] += w;
Splay.mx[y] += w;
}
int findroot(int x){
Access(x); Splay.splay(x); int y = x; while(Splay.L(y)){
y = Splay.L(y);
} return y;
} void makeroot(int x){
Access(x); Splay.splay(x); Splay.rev[x] ^= ;
} void cut(int x, int y){
makeroot(x); Access(y); Splay.splay(y);
Splay.L(y) = Splay.fa[Splay.L(y)] = ; Splay.pushup(y);
} void link(int x, int y){
makeroot(x); Splay.fa[x] = y; Splay.splay(x);
}
}lct; void insert(int s, int t){
++ tot;
u[tot] = s; v[tot] = t;
nxt[tot] = first[s];
first[s] = tot;
} void bfs(){
int head, tail;
head = tail = ;
que[head] = ;
Splay.fa[] = ; while(head <= tail){
int x = que[head]; for(int i = first[x]; i; i = nxt[i]){
if(v[i] != Splay.fa[x]){
Splay.fa[v[i]] = x;
que[++ tail] = v[i];
}
}
++ head;
}
} int main(){
int flag, x, y, z; while(~scanf("%d", &n)){
tot = ;
for(int i = ; i <= n; ++ i){
Splay.fa[i] = Splay.val[i] = Splay.add[i] = ;
Splay.rev[i] = false;
Splay.mx[i] = -oo;
Splay.c[i][] = Splay.c[i][] = ;
first[i] = ;
}
for(int i = ; i < n; ++ i){
scanf("%d%d", &x, &y);
insert(x, y); insert(y, x);
}
for(int i = ; i <= n; ++ i){
scanf("%d", &x);
Splay.val[i] = Splay.mx[i] = x;
}
bfs(); scanf("%d", &m);
for(int i = ; i <= m; ++ i){
scanf("%d", &flag);
switch(flag){
case :{
scanf("%d%d", &x, &y);
if(lct.findroot(x) == lct.findroot(y))
puts("-1");
else
lct.link(x, y);
break;
}
case :{
scanf("%d%d", &x, &y);
if(lct.findroot(x) != lct.findroot(y) || x == y)
puts("-1");
else lct.cut(x, y);
break;
}
case :{
scanf("%d%d%d", &z, &x, &y);
if(lct.findroot(x) != lct.findroot(y))
puts("-1");
else
lct.Add(x, y, z);
break;
}
case :{
scanf("%d%d", &x, &y);
if(lct.findroot(x) != lct.findroot(y))
puts("-1");
else{
lct.makeroot(x); lct.Access(y); Splay.splay(y);
printf("%d\n", Splay.mx[y]);
}
break;
}
}
}
puts("");
} return ;
}

HDU 4010

Link-Cut-Tree题目泛做(为了对应自己的课件)的更多相关文章

  1. K-D Tree题目泛做(CXJ第二轮)

    题目1: BZOJ 2716 题目大意:给出N个二维平面上的点,M个操作,分为插入一个新点和询问到一个点最近点的Manhatan距离是多少. 算法讨论: K-D Tree 裸题,有插入操作. #inc ...

  2. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  3. 洛谷P3690 [模板] Link Cut Tree [LCT]

    题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...

  4. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  5. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  6. Link Cut Tree 总结

    Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...

  7. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  8. [CodeForces - 614A] A - Link/Cut Tree

    A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...

  9. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

  10. 脑洞大开加偏执人格——可持久化treap版的Link Cut Tree

    一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现 ...

随机推荐

  1. 利用MiddleGen-hibernate-r5生成hbm文件及POJO文件

    1       先决条件 1.1    已安装JDK(版本1.5以上)并配置环境变量 到http://java.sun.com上下载JDK,配置环境变量(我的电脑右键->属性->高级-&g ...

  2. lightoj 1236 正整数唯一分解定理

    A - (例题)整数分解 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     6 ...

  3. hdu find the safest road

    算法:多源最短路(floyd) 题意:每条通路有一个安全系数,求始点到终点的最大的安全系数并输出,如果没有输出What a pity! c++超时啊 Problem Description XX星球有 ...

  4. C语言中的结构体和C++中的结构体以及C++中类的区别

    c++中结构体可以定义一个函数 C中的结构体和C++中结构体的不同之处:在C中的结构体只能自定义数据类型,结构体中不允许有函数,而C++中的结构体可以加入成员函数. C++中的结构体和类的异同: 一. ...

  5. Android开发笔记之: 数据存储方式详解

    无论是神马平台,神马开发环境,神马软件程序,数据都是核心.对于开发平台来讲,如果对数据的存储有良好的支持,那么对应用程序的开发将会有很大的促进作用.总体的来讲,数据存储方式有三种:一个是文件,一个是数 ...

  6. FCKeditor插件开发实例:uploadify多文件上传插件

    FCKeditor是一个专门使用在网页上属于开放源代码的所见即所得文字编辑器.它志于轻量化,不需要太复杂的安装步骤即可使用.它可和PHP.JavaScript.ASP.ASP.NET.ColdFusi ...

  7. Android 多渠道打包,上百渠道,秒打签名

    具体工具参见:https://github.com/hpu-spring87/MultiSignTools

  8. ViewDragHelper的使用

    一.ViewDragHelper的原理 是一个能够自用移动ViewGroup内部View的控件. 通过获取ViewGroup的点击事件,之后通过Scroller滑动来进行对ViewGroup内部控件的 ...

  9. Java学习笔记--JDBC数据库的使用

    参考  hu_shengyang的专栏 : http://blog.csdn.net/hu_shengyang/article/details/6290029 一. JDBC API中提供的常用数据库 ...

  10. 新版TeamTalk部署教程(蓝狐)

    http://www.bluefoxah.org/teamtalk/new_tt_deploy.html