link-cut tree

// It is made by XZZ
#include<cstdio>
#include<algorithm>
#define il inline
#define rg register
#define vd void
#define sta static
typedef long long ll;
il int gi(){
rg int x=0,f=1;rg char ch=getchar();
while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
const int maxn=10001;
namespace LCT{
int ch[maxn][2],fa[maxn];bool rev[maxn];
il vd down(int x){if(rev[x])rev[x]=0,rev[ch[x][0]]^=1,rev[ch[x][1]]^=1,std::swap(ch[x][0],ch[x][1]);}
il bool isrt(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
il vd rot(int x){
sta int y,z,o;
y=fa[x],z=fa[y],o=x==ch[y][1];
if(!isrt(y))ch[z][y==ch[z][1]]=x;fa[x]=z;
ch[y][o]=ch[x][!o];fa[ch[x][!o]]=y;
ch[x][!o]=y;fa[y]=x;
}
il vd splay(int x){
sta int stk[maxn],top;stk[top=1]=x;
for(rg int i=x;!isrt(i);i=fa[i])stk[++top]=fa[i];
while(top)down(stk[top--]);
for(rg int y=fa[x],z=fa[y];!isrt(x);rot(x),y=fa[x],z=fa[y])
if(!isrt(y))rot(((x==ch[y][1])^(y==ch[z][1]))?y:x);
}
il vd access(int x){for(rg int y=0;x;x=fa[y=x])splay(x),ch[x][1]=y;}
il vd makert(int x){access(x),splay(x);rev[x]^=1;}
il int find(int x){for(access(x),splay(x);ch[x][0];x=ch[x][0]);return x;}
il vd link(int x,int y){makert(x),fa[x]=y;}
il vd cut(int x,int y){makert(x);access(y),splay(y);fa[x]=ch[y][0]=0;}
}
int main(){
freopen("sdoi2008_cave.in","r",stdin);
freopen("sdoi2008_cave.out","w",stdout);
using namespace LCT;
int n,m,u,v;char opt[20],str[][5]={"No","Yes"};
n=gi(),m=gi();
while(m--){
scanf("%s",opt);u=gi(),v=gi();
if(opt[0]=='Q')puts(str[find(u)==find(v)]);
else if(opt[0]=='C')link(u,v);
else cut(u,v);
}
return 0;
}

常数有点小大。

cogs1889 [SDOI2008]Cave 洞穴勘测 link-cut tree的更多相关文章

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

    link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...

  2. BZOJ2049 SDOI2008 Cave 洞穴勘测 【LCT】

    BZOJ2049 SDOI2008 Cave 洞穴勘测 Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分 ...

  3. 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 link-cut-tree

    2016-05-30  11:04:51 学习了link-cut-tree 二中神犇封禹的讲义感觉讲的超级清晰易懂啊(没有的可以q窝 算是模板吧 #include<bits/stdc++.h&g ...

  4. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 LCT

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...

  5. 【LCT】BZOJ2049 [SDOI2008]Cave 洞穴勘测

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 10059  Solved: 4863[Submit ...

  6. BZOJ_2049_[Sdoi2008]Cave 洞穴勘测_LCT

    BZOJ_2049_[Sdoi2008]Cave 洞穴勘测_LCT Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由 ...

  7. bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...

  8. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (动态树入门)

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1528  Solved: 644[Submit][ ...

  9. 2049: [Sdoi2008]Cave 洞穴勘测

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MB Submit: 7475  Solved: 3499 [Submi ...

随机推荐

  1. 转:HTTP Status 404(The requested resource is not available)的几种解决方法

    原文地址 原因:servlet没有配置正确 ,查看web.xml确认正确,以及自己的请求路径正确 在IE中提示“404”错误有以下三种情况 1.未部署Web应用 2.URL输入错误 排错方法: 首先, ...

  2. 基于scrapyd爬虫发布总结

    一.版本情况 python以丰富的三方类库取得了众多程序员的认可,但也因此带来了众多的类库版本问题,本文总结的内容是基于最新的类库版本. 1.scrapy版本:1.1.0 D:\python\Spid ...

  3. JDBC方式执行SQL,支持CRUD返回LIST

    背景: 用惯了Mybatis,接收一个老项目使用Hibernate,特别不习惯.新的功能需要系统后台定时执行任务,顾使用JDBC封装工具类执行 源代码 import java.sql.Connecti ...

  4. Windows Server 2008搭建域控制器

    前言 1.为什么要建域 工作组的分散管理模式不适合大型的网络环境下工作,域模式就是针对大型的网络管理需求设计的,就是共享用户账号,计算机账号和安全策略的计算机集合.域中集中存储用户账号的计算机就是域控 ...

  5. CentOS服务器的加固方案

    >>>Centos账户安全 对Centos的加固首先要控制用户的权限,用户权限主要涉及到/etc下的/passwd,/shadow和/group三个文件 /passwd文件主要是存储 ...

  6. (1)访问控制 (2)final关键字 (3)对象创建的过程 (4)多态

    1.访问控制(笔试题)1.1 常用的访问控制符 public - 公有的 protected - 保护的 啥也不写 - 默认的 private - 私有的 1.2 访问控制符的比较 访问控制符 访问权 ...

  7. PHP设计模式系列 - 适配器

    什么是适配器: 适配器设计模式只是将某个对象的接口适配为另一个对象所期望的接口. 设计情景: 假如我们原始的有一个UserInfo的类,提供用户信息的类,早起设计该类的时候,只实现了一个getUser ...

  8. Hibernate学习笔记一之注解

    1.@Entiy  实体类注解 2.@Table  映射表 (name=“”)表名 3.@Coulmn @Column( name="columnName";            ...

  9. [消息传输123]ActiveMQ

    http://www.uml.org.cn/zjjs/201802111.asp https://www.cnblogs.com/cyfonly/p/6380860.html

  10. C# 通过word模板动态生成Word

    object oMissing = System.Reflection.Missing.Value; Word._Application oWord = new Word.Application(); ...