BZOJ 2049 & LCT又一模板
题意:
自己看...加边删边问联通...
SOL:
就加了一个findroot而已...
然而时间还是惨不忍睹...优化全开也才1700ms...膜seter...
Code:
- /*==========================================================================
- # Last modified: 2016-03-17 18:33
- # Filename: 2049.cpp
- # Description:
- ==========================================================================*/
- #define me AcrossTheSky
- #include <cstdio>
- #include <cmath>
- #include <ctime>
- #include <string>
- #include <cstring>
- #include <cstdlib>
- #include <iostream>
- #include <algorithm>
- #include <set>
- #include <map>
- #include <stack>
- #include <queue>
- #include <vector>
- #define lowbit(x) (x)&(-x)
- #define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++)
- #define FORP(i,a,b) for(int i=(a);i<=(b);i++)
- #define FORM(i,a,b) for(int i=(a);i>=(b);i--)
- #define ls(a,b) (((a)+(b)) << 1)
- #define rs(a,b) (((a)+(b)) >> 1)
- #define getlc(a) ch[(a)][0]
- #define getrc(a) ch[(a)][1]
- #define maxn 100000
- #define maxm 100000
- #define pi 3.1415926535898
- #define _e 2.718281828459
- #define INF 1070000000
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- template<class T> inline
- void read(T& num) {
- bool start=false,neg=false;
- char c;
- num=0;
- while((c=getchar())!=EOF) {
- if(c=='-') start=neg=true;
- else if(c>='0' && c<='9') {
- start=true;
- num=num*10+c-'0';
- } else if(start) break;
- }
- if(neg) num=-num;
- }
- /*==================split line==================*/
- int ch[maxn][2],rev[maxn],nxt[maxn],fa[maxn],pre[maxn],s[maxn];
- int sta[maxn];
- int n,m;
- inline void pushup(int x){
- s[x]=1+s[ch[x][0]]+s[ch[x][1]];
- }
- inline void pushdown(int x){
- if (rev[x]){
- swap(ch[x][0],ch[x][1]);
- rev[ch[x][0]]^=1;
- rev[ch[x][1]]^=1;
- rev[x]=0;
- }
- }
- inline void setx(int x){
- if (ch[x][1]) {
- fa[ch[x][1]]=0; pre[ch[x][1]]=x; ch[x][1]=0; pushup(x);
- }
- }
- inline void rotate(int x){
- int p=fa[x],q=fa[p],d=ch[p][1]==x;
- fa[ch[p][d]=ch[x][d^1]]=p; pushup(p);
- fa[ch[x][d^1]=p]=x; pushup(x);
- fa[x]=q;
- if (q){
- if (ch[q][1]==p) ch[q][1]=x;
- else ch[q][0]=x;
- }
- else pre[x]=pre[p];
- }
- inline void splay(int x){
- int top=0;
- for (int i=x;i;i=fa[i]) sta[++top]=i;
- for (;top;top--) pushdown(sta[top]);
- for (int y;(y=fa[x])!=0;rotate(x))
- if (fa[y]) rotate((getlc(y)==x)==(getlc(fa[y])==y)?y:x);
- }
- inline void access(int x){
- splay(x); setx(x); int v;
- while(v=pre[x]){
- splay(v); setx(v);
- ch[v][1]=x; fa[x]=v; pushup(v);
- x=v;
- }
- }
- inline void beroot(int x){
- access(x);
- splay(x);
- rev[x]^=1;
- }
- inline void link(int x,int y){
- beroot(x);
- pre[x]=y;
- //access(x); splay(x);
- }
- inline void cut(int x,int y){
- beroot(x); access(y); splay(y);
- ch[y][0]=fa[x]=pre[x]=0;
- pushup(y);
- }
- inline int findroot(int x){
- while (ch[x][0]) x=ch[x][0];
- return x;
- }
- inline void query(int x,int y){
- beroot(y);
- access(x);
- splay(x);
- if (findroot(x)==y) printf("Yes\n");
- else printf("No\n");
- }
- int main(){
- read(n); read(m);
- FORP(i,1,n) s[i]=1;
- FORP(i,1,m){
- char temp[10]; int x,y;
- scanf("%s",temp); read(x); read(y);
- if (x<y) swap(x,y);
- if (temp[0]=='Q') query(x,y);
- else if (temp[0]=='C') link(x,y);
- else cut(x,y);
- }
- }
BZOJ 2049 & LCT又一模板的更多相关文章
- BZOJ 2049 LCT
思路:LCT的基本操作 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm&g ...
- [BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】
题目链接:BZOJ - 2049 题目分析 LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内. Link(x, y) : Make_Root(x); Splay(x); F ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测——LCT
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 省选之前来切一道数据结构模板题. 题意 这是一道模板题. N个点,M次操作,每次加边/ ...
- bzoj 2049: [Sdoi]Cave 洞穴探测 (LCT)
第一次写lct (这是一道lct裸题 这次没有可爱(划掉)的同学教我,虽然有模板,但是配合网上的讲解还是看不懂QAQ 然后做了几道题之后总算有些感觉辣 于是决定给自己挖个坑,近期写一个lct详解(不过 ...
- bzoj 2049 [Sdoi2008]Cave 洞穴勘测(LCT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2049 [题意] 给定森林,可能有连边或断边的操作,回答若干个连通性的询问. [思路] ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘測 LCT
入门级LCT: 仅仅有 Cut Link 2049: [Sdoi2008]Cave 洞穴勘測 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 3073 ...
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...
- BZOJ 2049 SDOI2008 洞穴勘测 LCT板子
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题意概述:给出N个点,一开始不连通,M次操作,删边加边,保证图是一个森林,询问两点连 ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 LCT
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...
随机推荐
- Python中format的用法
自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱.语法 它通过{}和: ...
- hdu 1754:I Hate It(线段树,入门题,RMQ问题)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Could not link against boost_system 解决办法
Could not link against boost_system 解决办法: 先安装 libboost-all-dev ./configure --with-incompatible-bdb - ...
- 运维自动化之ansible的安装与使用(包括模块与playbook使用)(转发)
原文 http://dl528888.blog.51cto.com/2382721/1435415 我使用过puppet(地址是http://dl528888.blog.51cto.com/2382 ...
- Java 初学记录之一 快速输入
1. sysout 按回车 System.out.println();
- 【leetcode】Reverse Words in a String
今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...
- 在ASP.NET 5中使用SignalR
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:SignalR作为ASP.NET中进行Web实时双向通信的组件,在ASP.NET 5中 ...
- 在Virtulbox上装Ubuntu
做个程序员,会用Linux,这应该是最基本的要求吧.可惜本人经常用Windows,只是偶尔去服务器上做些操作的时候才接触到linux.so,我要学Linux.刚学所以还是先装个虚拟机吧,等在虚拟机上用 ...
- hpunix下11gRac的安装
一.检查环境 1.操作系统版本# uname -a 2.补丁包三大补丁包#swlist -l bundle|grep QPKAPPS#swlist -l bundle|grep QPKBASE#swl ...
- POJ 1163:The Triangle
Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a progr ...