Link-Cut-Tree题目泛做(为了对应自己的课件)
题目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题目泛做(为了对应自己的课件)的更多相关文章
- K-D Tree题目泛做(CXJ第二轮)
题目1: BZOJ 2716 题目大意:给出N个二维平面上的点,M个操作,分为插入一个新点和询问到一个点最近点的Manhatan距离是多少. 算法讨论: K-D Tree 裸题,有插入操作. #inc ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
- 洛谷P3690 [模板] Link Cut Tree [LCT]
题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- Link Cut Tree学习笔记
从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...
- Link Cut Tree 总结
Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...
- link cut tree 入门
鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...
- [CodeForces - 614A] A - Link/Cut Tree
A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...
- 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)
题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
- 脑洞大开加偏执人格——可持久化treap版的Link Cut Tree
一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现 ...
随机推荐
- unity绘制线和绘制面
绘制线条代码,其实就是指定至少两个点,然后赋予贴图即可,不废话,上代码: using UnityEngine; using System.Collections; public class LineT ...
- 2 MD5加密 java实现
百度百科对MD5的说明是: Message Digest Algorithm MD5(中文名为消息摘要算法第 五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护. MD5即Mess ...
- Shell脚本中单引号(‘)和双引号(“)的使用区别[转载]
shell可以识别4种不同类型的引字符号: 单引号字符' 双引号字符" 反斜杠字符\ 反引号字符` 1. 单引号 ( '' ) # grep Susan phonebook Susan Go ...
- (原).cc 和 .cpp 后缀结尾的文件的区别
This caused a few problems the first time C++ was ported to a system where case wasn't significant i ...
- DOM 节点实例操作
涉及知识点包括节点的所有知识 目的: 自动为文档创建一个目录表 自动创建目录
- 深入理解Azure自动扩展集VMSS(2)
VMSS中Auto Scale基本原理及诊断 在前面的介绍中,我们看到通过定义规则可以实现虚拟机扩展集的auto scale,那么在后台执行上VMSS的扩展依赖于哪些组件,出现问题(比如自动扩展没有发 ...
- 在Fedora 20下编译安装QEMU
由于OpenStack的流行,现在越来越多的人关注QEMU,最近发行了新版本1.7,下面就说Fedora下的安装过程: 首先需要安装基本的编译软件和git sudo yum install gcc m ...
- 伪造队形(FFT)
题目描述Tukkun带着他的合唱队去环形音乐厅参加演出.上场前,Tukkun发现了严重的问题:音乐厅的工作人员把他们的合唱队形搞错了.具体来说,Tukkun的合唱队有N个人围成一圈,身高按照顺时针顺序 ...
- UML--核心元素之业务实体
如果说参与者和用例描述了我们在这个问题领域中达到什么样的目标,那么业务实体就描述了我们使用什么来达到业务目标以及通过什么来记录这个业务目标. 如果把问题领域比喻成一幢大楼的话,业务实体就是构成这幢大楼 ...
- 2013第46周四xml作为WS两端中间测试文件
今天又到了11点多才开始写随笔记录,有点惭愧,加班回来到现在已经近2小时了,而我此刻才进入正题,之前的时间都跟MM聊天了,或许是单身久了,居然在个人情感方面感觉自己很out了,不想这些了,重点是回顾下 ...