zoukankan      html  css  js  c++  java
  • bzoj 2049: [Sdoi]Cave 洞穴探测 (LCT)

    第一次写lct

    (这是一道lct裸题

    这次没有可爱(划掉)的同学教我,虽然有模板,但是配合网上的讲解还是看不懂QAQ

    然后做了几道题之后总算有些感觉辣

    于是决定给自己挖个坑,近期写一个lct详解(不过像我这么懒的人= =

    下面是代码

     1 /**************************************************************
     2     Problem: 2049
     3     User: cminus
     4     Language: C++
     5     Result: Accepted
     6     Time:1532 ms
     7     Memory:984 kb
     8 ****************************************************************/
     9  
    10 #include <cstdio>
    11 #include <algorithm>
    12 #include <stack>
    13 using namespace std;
    14 #define kd(x) (ch[fa[x]][1] == x)
    15 #define setc(f, c, k) (ch[fa[c] = f][k] = c)
    16 #define isRoot(x) (ch[fa[x]][0] != x and ch[fa[x]][1] != x)
    17 #define ls ch[x][0]
    18 #define rs ch[x][1]
    19 const int N = 10020;
    20  
    21 int fa[N], ch[N][2], n, m;
    22 bool mark[N];
    23  
    24 inline void push(int x){
    25     if (mark[x]){
    26         mark[x] = false;
    27         mark[ls] ^= 1;
    28         mark[rs] ^= 1;
    29         swap(ls, rs);
    30     } 
    31 }
    32 stack < int > S;
    33 inline void pushDown(int x) {
    34     S.push(x);
    35     while (!isRoot(x))
    36         S.push(x = fa[x]);
    37     while (!S.empty())
    38         push(S.top()), S.pop();
    39 }
    40  
    41 inline void rotate (int x) {
    42     int y = fa[x], k = kd(x);
    43     setc(y, ch[x][k ^ 1], k);
    44     if (isRoot(y)) fa[x] = fa[y];
    45     else setc(fa[y], x, kd(y));
    46     setc(x, y, k ^ 1);
    47 }
    48  
    49 inline void splay (int x) {
    50     pushDown(x);
    51     while (! isRoot(x)) {
    52         if (! isRoot(fa[x]))
    53             if (kd(x) == kd(fa[x])) rotate(fa[x]);
    54             else rotate(x);rotate(x);
    55     }
    56 }
    57  
    58 inline void access(int x) {
    59     int t = 0;
    60     while (x) {
    61         splay(x);
    62         rs = t; t = x; x = fa[x];
    63     }
    64 }
    65  
    66 inline void makeRoot(int u) {
    67     access(u); splay(u);
    68     mark[u] ^= 1;
    69 }
    70  
    71 inline void link(int u, int v){
    72     makeRoot(u); fa[u] = v;
    73 }
    74  
    75 inline void cut(int u, int v){
    76     makeRoot(u);
    77     access(v);  splay(v);
    78     fa[u] = ch[v][0] = 0; 
    79 }
    80  
    81 inline int findRoot(int x) {
    82     while(fa[x])    x = fa[x];
    83     return x;
    84 }
    85 int main(){
    86     scanf("%d %d", &n, &m);
    87     char str[20];
    88     while(m--){
    89         int u, v;
    90         scanf("%s %d %d", str, &u, &v);
    91         if (*str == 'C')    link(u, v);
    92         else if (*str == 'D')   cut(u, v);
    93         else    puts((findRoot(u) == findRoot(v)) ? "Yes" : "No");
    94     } 
    95     return 0;
    96 }
  • 相关阅读:
    【原】error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string'
    【原】开发路上疑难BUG调试记录
    【转】XCode: duplicate symbol 解决方案
    【原】就IOS发布app时如何保护文本资源的一个方法
    【原】xcode5.0升级5.1遇到的clang: error: unknown argument: '-fobj-arc'错误
    【转】生产者与消费者
    安卓快速排序与冒泡排序
    数据结构之算法时间复杂度
    常见数据结构与算法整理总结(上)
    Activity的onPause()、onStop()和onDestroy()里要做的事情
  • 原文地址:https://www.cnblogs.com/cminus/p/7056221.html
Copyright © 2011-2022 走看看