link cut tree 入门

鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构。

link cut tree:研究popoqqq那个神ppt。

bzoj1036:维护access操作就可以了。

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<queue>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
#define qwq(x) for(edge *o=head[x];o;o=o->next)
int read(){
    int x=0,f=1;char c=getchar();
    while(!isdigit(c)){
        if(c==‘-‘) f=-1;c=getchar();
    }
    while(isdigit(c)) x=x*10+c-‘0‘,c=getchar();
    return x*f;
}
const int nmax=3e4+5;
const int inf=0x7f7f7f7f;
struct edge{
    int to;edge *next;
};
edge es[nmax<<1],*pt=es,*head[nmax];
void add(int u,int v){
    pt->to=v;pt->next=head[u];head[u]=pt++;
    pt->to=u;pt->next=head[v];head[v]=pt++;
}
int n,m,fa[nmax],c[nmax][2],sm[nmax],mx[nmax],q[nmax],w[nmax];bool vis[nmax];
void bfs(){
    int l=1,r=1,x;q[r]=1;vis[1]=1;
    while(l<=r){
        x=q[l++];
        qwq(x) if(!vis[o->to]) fa[o->to]=x,q[++r]=o->to,vis[o->to]=1;
    }
}
bool pdrt(int x){
    return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
}
void update(int x){
    int l=c[x][0],r=c[x][1];
    sm[x]=sm[l]+sm[r]+w[x];mx[x]=max(w[x],max(mx[l],mx[r]));
}
void rotate(int x){
    int y=fa[x],z=fa[y],l=c[y][1]==x,r=l^1;
    if(!pdrt(y)) if(c[z][0]==y) c[z][0]=x;else c[z][1]=x;
    fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
    c[y][l]=c[x][r];c[x][r]=y;
    update(y);update(x);
}
void splay(int x){ //把一个点伸展到它所在的重链的根。
    int y,z;//fa[x]=y表示以x为根的splay树的父亲是y,但是y的两个儿子(c[y][0]和c[y][1])中没有一个是x
    while(!pdrt(x)){
        y=fa[x];z=fa[y];
        if(!pdrt(y)) if(c[y][0]==x^c[z][0]==y) rotate(x);else rotate(y);
        rotate(x);
    }
}
int access(int x){
    int t=0;
    while(x) splay(x),c[x][1]=t,t=x,update(x),x=fa[x];
    return t;
    //考虑点u,v。假设先access(u)了,那么access(lca(u,v))时lca(u,v)是原splay的根节点,
    //左边的是深度比它小的点,即是lca(u,v)到根的链。 说明access的操作是正确的。
}
void qsum(int u,int v){
    access(u);int lca=access(v);splay(u);
    if(lca==u) printf("%d\n",w[u]+sm[c[lca][1]]);
    else printf("%d\n",w[lca]+sm[c[lca][1]]+sm[u]);
}
void qmax(int u,int v){
    access(u);int lca=access(v);splay(u);
    if(lca==u) printf("%d\n",max(w[u],mx[c[lca][1]]));
    else printf("%d\n",max(w[lca],max(mx[c[lca][1]],mx[u])));
}
int main(){
    n=read();int u,v,d;mx[0]=-inf;
    rep(i,1,n-1) u=read(),v=read(),add(u,v);
    rep(i,1,n) sm[i]=mx[i]=w[i]=read();bfs();
    m=read();char ch[11];
    rep(i,1,m){
        scanf("%s",ch);u=read(),v=read();
        if(ch[1]==‘H‘) splay(u),w[u]=v,update(u);
        else if(ch[1]==‘S‘) qsum(u,v);
        else qmax(u,v);
    }
    return 0;
}

bzoj2049:link cut tree 模版题。

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
#define qwq(x) for(edge *o=head[x];o;o=o->next)
int read(){
    int x=0,f=1;char c=getchar();
    while(!isdigit(c)){
        if(c==‘-‘) f=-1;c=getchar();
    }
    while(isdigit(c)) x=x*10+c-‘0‘,c=getchar();
    return x*f;
}
const int nmax=1e4+5;
int n,m,fa[nmax],c[nmax][2],st[nmax],rev[nmax];
bool pdrt(int x){
    return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
}
void pushdown(int x){
    if(rev[x]){
        int l=c[x][0],r=c[x][1];
        rev[x]^=1;rev[l]^=1;rev[r]^=1;
        swap(c[x][0],c[x][1]);
    }
}
void rotate(int x){
    int y=fa[x],z=fa[y],l=c[y][1]==x,r=l^1;
    if(!pdrt(y)) if(c[z][0]==y) c[z][0]=x;else c[z][1]=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){
    int top=0,y,z;st[++top]=x;
    for(int i=x;!pdrt(i);i=fa[i]) st[++top]=fa[i];
    dwn(i,top,1) pushdown(st[i]);
    while(!pdrt(x)){
        y=fa[x];z=fa[y];
        if(!pdrt(y)) if(c[y][0]==x^c[z][0]==y) rotate(x);else rotate(y);
        rotate(x);
    }
}
void access(int x){
    int t=0;
    while(x) splay(x),c[x][1]=t,t=x,x=fa[x];
}
void rever(int x){
    access(x);splay(x);rev[x]^=1;
}
void link(int x,int y){
    rever(x);fa[x]=y;
}
void cut(int x,int y){
    rever(x);access(y);splay(y);c[y][0]=fa[x]=0;
}
int find(int x){
    access(x);splay(x);
    int y=x;while(c[y][0]) y=c[y][0];
    return y;
}
int main(){
    n=read(),m=read();int x,y;char ch[11];
    rep(i,1,m){
        scanf("%s",ch);x=read(),y=read();
        if(ch[0]==‘C‘) link(x,y);
        else if(ch[0]==‘D‘) cut(x,y);
        else {
            if(find(x)==find(y)) puts("Yes");
            else puts("No");
        }
    }
    return 0;
}

我这二逼智商。。。真的够了。。。。

时间: 2024-08-07 16:38:50

link cut tree 入门的相关文章

bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isroot(int x):判断x是否为所在重链(splay)的根 void down(int x):下放各种标记 void rotate(int x):在x所在重链(splay)中将x旋转到fa[x]的位置上 void splay(int x):在x坐在重链(splay)中将x旋转到根 void acce

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

A. Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure. Unfortunately, Rostislav is unable to understand the definition

AC日记——【模板】Link Cut Tree 洛谷 P3690

[模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300005 int n,m,val[maxn]; int top,ch[maxn][2],f[maxn],xr[maxn],q[maxn],rev[maxn]; inline void in(int &now) { int if_z=1;now=0; char Cget=getchar(); while

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

一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现还是可以写的,只需要实时交换答案二元组里的两棵树,最后在吧提出来的访问节点放回去就行了.本着只学一种平衡树的想法,脑洞大开加偏执人格的开始写可持久化Treap版的Link Cut Tree... 写了才发现,常数硕大啊!!!代码超长啊!!!因为merge是从上到下,split从下到上,pushdow

HDOJ 题目3966 Aragorn&#39;s Story(Link Cut Tree成段加减点权,查询点权)

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5505    Accepted Submission(s): 1441 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的. 1:后接两个整数(x,y),代表连接x到y,若x到y已经联通则无需连接. 2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在. 3:后接两个整数(x,y),代表将点x上的权值变成y. 输入输出

Link Cut Tree学习笔记

从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子树信息 小结 动态树问题和Link Cut Tree 动态树问题是一类要求维护一个有根树森林,支持对树的分割, 合并等操作的问题. Link Cut Tree(林可砍树?简称LCT)是解决这一类问题的一种数据结构. 一些无聊的定义 Link Cut Tree维护的是动态森林中每棵树的任意链剖分. P

P3690 【模板】Link Cut Tree (动态树)

P3690 [模板]Link Cut Tree (动态树) https://www.luogu.org/problemnew/show/P3690 分析: LCT模板 代码: 注意一下cut! 1 #include<cstdio> 2 #include<algorithm> 3 4 using namespace std; 5 6 const int N = 300100; 7 8 int val[N],fa[N],ch[N][2],rev[N],sum[N],st[N],top;

Link Cut Tree(无图慎入)

类似树链剖分(其实直接记住就可以了),提前放代码 1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 #include<algorithm> 5 #include<cstring> 6 #include<climits> 7 #include<cmath> 8 #define N (int)(3e5+5) 9 using namespace std;