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.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现 ...
随机推荐
- 不用派生CTreeCtrl不用繁琐的过程 教你如何让CTreeCtrl的每一项有ToolTip提示
最近工作中需要让CTreeCtrl控件的每一项都有提示信息,于是谷歌百度,爬山涉水,结果是………….在CodeProject里找到一篇文章是把CTreeCtrl派生出新类,重载一些函数自定义内容.使用 ...
- Maven基础教程
更多内容请参考官方文档:http://maven.apache.org/guides/index.html 官方文档很详细,基本上可以查找到一切相关的内容. 另外,快速入门可参考视频:孔浩的maven ...
- Android 多渠道打包,上百渠道,秒打签名
具体工具参见:https://github.com/hpu-spring87/MultiSignTools
- 03--理解HelloWorld结构
作为程序猿还是要代码来说事的,现在开始进入到具体的代码中来.国际惯例HelloWorld打头阵,我也不能免这个俗. Win32入口函数中主要代码如下: main.cpp // 创建应用实例 AppDe ...
- nginx对于Yii2的前后台的配置
虽然是个基础问题,但也折腾了不少时间,记录下来,希望对新手有所帮助.例如我的前台地址yooao.cc,后台地址back.yooao.cc back.yooao.cc是yooao.cc的一个子域名. ...
- ACboy needs your help(HDU 1712 分组背包入门)
ACboy needs your help Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- DataTables在回调方法中使用api
$(document).ready(function() { $('#example').dataTable( { "initComplete": function () { va ...
- ng-repeat-start ng-repeat-end 的使用
ng-repeat-start与ng-repeat-end时AngularJS(1.2.x)扩展的, 使用这两个指令可以灵活控制遍历形式. 例如: index.html <div class=& ...
- Visual Studio 2013 Professional Key
今天发现家里的VS2013专业版过期了,于是google百度一顿大搜,多数key都不能用,不过还是找到一个key可以使用的. Visual Studio 2013 Professional Key: ...
- linux find详解
find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> - < ...