cogs1889 [SDOI2008]Cave 洞穴勘测 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的更多相关文章
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...
- BZOJ2049 SDOI2008 Cave 洞穴勘测 【LCT】
BZOJ2049 SDOI2008 Cave 洞穴勘测 Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分 ...
- 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 link-cut-tree
2016-05-30 11:04:51 学习了link-cut-tree 二中神犇封禹的讲义感觉讲的超级清晰易懂啊(没有的可以q窝 算是模板吧 #include<bits/stdc++.h&g ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 LCT
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...
- 【LCT】BZOJ2049 [SDOI2008]Cave 洞穴勘测
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 10059 Solved: 4863[Submit ...
- BZOJ_2049_[Sdoi2008]Cave 洞穴勘测_LCT
BZOJ_2049_[Sdoi2008]Cave 洞穴勘测_LCT Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由 ...
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (动态树入门)
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1528 Solved: 644[Submit][ ...
- 2049: [Sdoi2008]Cave 洞穴勘测
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 7475 Solved: 3499 [Submi ...
随机推荐
- sharepoint 2013 service pack 和 Hotfix 版本
方法1:Central Administration > System Settings > Manage servers in your farm (/_admin/FarmServer ...
- October 1st 2017 Week 40th Sunday
It's only after we've lost everything that we're free to do anything. 只有我们失去了所有之后我们才能随心而为. After los ...
- 微信公众平台OAuth2.0授权
1. 配置授权回调页面域名 进入微信公众平台后台后,依次进入开发者中心-权限表,找到网页授权获取用户基本信息,点击右侧的修改. 授权回调域名配置规范为全域名并且不带http,比如需要网页授权的域名为: ...
- iPhone X进入DFU模式
下面把iphoneX进dfu模式的方法分享给大家.在开机状态下,按一下音量加键后松开,然后按一下音量减键后松开,接着按住电源键,屏幕完全熄灭后,松开电源键,接着同时按住电源键和音量减,保持5秒左右,再 ...
- 3、JVM--垃圾回收期和内存分配策略(1)
前言: Java与C++之间有一堵由内存动态分配和垃圾收集技术所围成的“高墙”,墙外面的人想进去,墙里面的人却想出来. 3.1.概述 垃圾收集(Garbage Collection,GC),大部分人都 ...
- 【转】JS实现继承的几种方式
既然要实现继承,那么首先我们得有一个父类,代码如下: // 定义一个动物类 function Animal (name) { // 属性 this.name = name || 'Animal'; / ...
- python list 使用技巧
格式:list[start:stop:step] 示例:a =list(range(0,10))print(a[1:8:2]) #[1, 3, 5, 7]print(a[:8:2]) #[0, 2, ...
- 关于WebSocket长链接的详细介绍iOS
http://www.cocoachina.com/ios/20181030/25327.html
- 897. Increasing Order Search Tree
题目来源: https://leetcode.com/problems/increasing-order-search-tree/ 自我感觉难度/真实难度:medium/easy 题意: 分析: 自己 ...
- unittest框架官方翻译:中文版
指数 模块 | 下一个 | 上一个 | Python » 英语 法国 日本 朝鲜的 开发(3.8) 3.7.3rc1 3.6 3.5 2.7 文档 » Python标准库 » 开发工具 » | ...