博客
关于我
【LCT】P2147 [SDOI2008]洞穴勘测
阅读量:293 次
发布时间:2019-03-03

本文共 3125 字,大约阅读时间需要 10 分钟。

LCT(链接集合树)是一种高效的数据结构,广泛应用于处理各种树状结构的操作问题。以下是基于LCT的实现代码解析与应用示例。

代码结构分析:

  • 数据结构定义

    • 使用二叉树结构表示节点,每个节点包含父指针、左右子树指针以及逆序标记。
    • 树的大小限制为maxn,每个节点初始化时父指针、左右子树指针以及逆序标记都初始化为0。
  • 操作函数

    • isroot:判断一个节点是否为根节点。
    • rev:交换子树方向,并翻转逆序标记。
    • pushdown:将当前节点的子树操作下放到子节点。
    • rotate:旋转节点,使其成为新的根节点。
    • splay:通过旋转操作使树深度减少,提高操作效率。
    • access:通过路径压缩找到目标节点。
    • makeroot:将某个节点设置为根节点,并进行逆序调整。
    • findroot:找到当前树的根节点。
    • link:将两个树连接,形成新的树结构。
    • cut:将两个树分开,确保无环。
  • 代码应用示例:

    #include 
    using namespace std;const int maxn = 2e5;int n, m;struct LCT { struct node { int fa, ch[2], rev; node() { fa = ch[0] = ch[1] = rev = 0; } } tree[maxn]; int top, res[maxn]; #define ls(x) tree[x].ch[0] #define rs(x) tree[x].ch[1] int isroot(int x) { return tree[x].fa && (ls(tree[x].fa) == x || rs(tree[x].fa) == x); } void rev(int x) { swap(ls(x), rs(x)); tree[x].rev ^= 1; } void pushdown(int x) { if (!tree[x].rev) return; if (ls(x)) rev(ls(x)); if (rs(x)) rev(rs(x)); tree[x].rev ^= 1; } void rotate(int x) { int y = tree[x].fa, z = tree[y].fa; int k = (x == ls(tree[x].fa)), w = tree[x].ch[k]; if (isroot(y)) tree[z].ch[y == rs(tree[y].fa)] = x; tree[x].ch[k] = y; tree[y].ch[!k] = w; tree[y].fa = x; tree[x].fa = z; if (w) tree[w].fa = y; } void splay(int x) { int tmp = x; res[top = 1] = tmp; while (isroot(tmp)) { res[++top] = tree[tmp].fa; tmp = tree[tmp].fa; } while (top) { pushdown(res[top]); top--; } while (isroot(x)) { if (isroot(tree[x].fa)) { int k = (x == rs(tree[x].fa)) ^ (tree[x].fa == rs(tree[tree[x].fa].fa)); rotate(k ? tree[x].fa : x); } rotate(x); } } void access(int x) { for (int y = 0; x; x = tree[x].fa) { splay(x); rs(x) = y; } } void makeroot(int x) { access(x); splay(x); rev(x); } int findroot(int x) { access(x); splay(x); while (ls(x)) { pushdown(x); x = ls(x); pushdown(x); } return x; } void link(int x, int y) { if (findroot(x) == findroot(y)) return; makeroot(x); tree[x].fa = y; } void cut(int x, int y) { makeroot(x); access(y); splay(y); if (ls(y) == x) { tree[x].fa = ls(y) = 0; } }} LCT;int main() { freopen("a.in", "r", stdin); freopen("a.out", "w", stdout); scanf("%d%d", &n, &m); char op[20]; int x, y; for (int i = 1; i <= m; ++i) { scanf("%s", op); scanf("%d%d", &x, &y); if (op[0] == 'C') { LCT.link(x, y); } else if (op[0] == 'D') { LCT.cut(x, y); } else if (op[0] == 'Q') { if (LCT.findroot(x) == LCT.findroot(y)) { printf("Yes\n"); } else { printf("No\n"); } } } return 0;}

    以上代码实现了一个高效的LCT结构,支持常规的树操作,如连接、切割以及根节点查询等功能。通过路径压缩和旋转操作,LCT显著提升了树操作的效率,使得复杂度接近线性。

    转载地址:http://idwm.baihongyu.com/

    你可能感兴趣的文章
    NIO与零拷贝和AIO
    查看>>
    NIO同步网络编程
    查看>>
    NIO基于UDP协议的网络编程
    查看>>
    NIO笔记---上
    查看>>
    NIO蔚来 面试——IP地址你了解多少?
    查看>>
    NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
    查看>>
    NISP国家信息安全水平考试,收藏这一篇就够了
    查看>>
    NIS服务器的配置过程
    查看>>
    Nitrux 3.8 发布!性能全面提升,带来非凡体验
    查看>>
    NiuShop开源商城系统 SQL注入漏洞复现
    查看>>
    NI笔试——大数加法
    查看>>
    NLog 自定义字段 写入 oracle
    查看>>
    NLog类库使用探索——详解配置
    查看>>
    NLP 基于kashgari和BERT实现中文命名实体识别(NER)
    查看>>
    NLP 模型中的偏差和公平性检测
    查看>>
    Vue3.0 性能提升主要是通过哪几方面体现的?
    查看>>
    NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
    查看>>
    NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
    查看>>
    NLP三大特征抽取器:CNN、RNN与Transformer全面解析
    查看>>
    NLP学习笔记:使用 Python 进行NLTK
    查看>>