[BJOI2014]大融合 LCT维护子树信息

本文深入探讨了LCT树(Link-Cut Tree)的数据结构原理及其在算法竞赛中的实际应用。通过C++代码实现,详细讲解了如何利用LCT树进行节点链接、查询等操作,为解决复杂路径和子树问题提供了高效算法方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Code:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
void setIO(string a){freopen((a+".in").c_str(),"r",stdin);}

#define maxn 100009
#define ll long long
int n,q;
struct LCT{
    int ch[maxn][2],f[maxn],siz[maxn],sta[maxn],son[maxn],tag[maxn];
    int lson(int x){ return ch[x][0]; }
    int rson(int x){ return ch[x][1]; }
    int get(int x){ return ch[f[x]][1]==x;}
    int isRoot(int x){ return !(ch[f[x]][0]==x||ch[f[x]][1]==x); }
    void mark(int x){ if(!x)return; swap(ch[x][0],ch[x][1]),tag[x]^=1; }
    void pushdown(int x){ if(tag[x]) mark(lson(x)),mark(rson(x)),tag[x]=0; }
    void pushup(int x){ if(!x) return; siz[x]=siz[lson(x)]+siz[rson(x)]+son[x]+1; }

    void rotate(int x){
        int old=f[x],fold=f[old],which=get(x);
        if(!isRoot(old)) ch[fold][ch[fold][1]==old]=x; 
        ch[old][which]=ch[x][which^1],f[ch[old][which]]=old;
        ch[x][which^1]=old,f[old]=x,f[x]=fold;
        pushup(old),pushup(x); 
    }
    void splay(int x){
        int v=0,u=x;
        sta[++v]=u;
        while(!isRoot(u)) sta[++v]=f[u],u=f[u];
        while(v) pushdown(sta[v--]);
        u=f[u];
        for(int fa;(fa=f[x])!=u;rotate(x))
            if(f[fa]!=u) rotate(get(fa)==get(x)?fa:x);
    }
    void Access(int x){for(int y=0;x;y=x,x=f[x]) splay(x),son[x]=son[x]+siz[ch[x][1]]-siz[y], ch[x][1]=y, pushup(x);  }
    void makeRoot(int x){ Access(x), splay(x), mark(x);}
    void link(int a,int b){ makeRoot(a), makeRoot(b), son[a]+=siz[b], f[b]=a,  pushup(a); }
    ll query(int a,int b){
        makeRoot(a),makeRoot(b); 
        return (long long)siz[a]*(siz[b]-siz[a]);
    }
}tree;
int main(){
    //setIO("input");
    int a,b;
    char opt[5];
    scanf("%d%d",&n,&q);
    while(q--){
        scanf("%s%d%d",opt,&a,&b);
        switch(opt[0]){
            case 'A': {
                tree.link(a,b);
                break;
            }
            case 'Q': {
                printf("%lld\n",tree.query(a,b));
                break;
            }
        }
    }
    return 0;
}

  

### BJOI2013 压力 题目解析 #### 问题描述 题目要求计算每个网络设备必须通过的数据包数量。给定一个无向图,其中存在 $ N $ 个节点和 $ M $ 条边,以及 $ Q $ 组询问,每组询问表示从某个源点到目标点之间的路径。需要统计哪些节点是这些路径中的必经之点。 此问题可以通过构建 **圆方树** 并利用其特性来解决[^1]。 --- #### 圆方树简介 圆方树是一种基于无向图的特殊结构,能够高效处理与割点和桥有关的问题。它由两类节点组成: - **圆形节点**:代表原图中的实际顶点。 - **方形节点**:对应于原图的一个双连通分量 (BCC),即一组不存在割点的顶点集合。 在该题中,我们需要关注的是如何标记并统计经过特定割点的路径数目[^4]。 --- #### 实现细节 以下是具体实现方法: 1. **构建圆方树** 使用 Tarjan 算法找到所有的割点及其对应的双连通分量,并以此为基础构造圆方树。对于每一个新发现的双连通分量,创建一个新的方形节点并与所属的割点相连。 2. **路径差分** 对于每次查询 $(u, v)$,将其转化为对圆方树上的一次简单路径操作。通过对路径上的所有割点执行加一的操作完成统计工作[^2]。 3. **线段树优化** 考虑到可能存在的量修改请求,在最终阶段可以引入线段树或其他区间数据结构进一步加速更新过程。 下面给出一段伪代码展示上述逻辑的核心部分: ```python def tarjan(u, fa): dfn[u] = low[u] = time_stamp stk.append(u) for y in adj[u]: if not dfn[y]: tarjan(y, u) low[u] = min(low[u], low[y]) if low[y] >= dfn[u]: # Found articulation point or bridge build_bcc(u, y) # Build corresponding square node elif y != fa and dfn[y] < dfn[u]: low[u] = min(low[u], dfn[y]) def build_bcc(root, child): global poi r = ++poi while True: w = stk[-1] stk.pop() att(r, w) # Attach the vertex to current biconnected component if w == child: break att(r, root) # Query processing using tree difference technique on constructed round-square tree. for query in queries: path_diff(query.start, query.end) ``` --- #### 时间复杂度分析 整个算法的时间复杂度主要依赖以下几个方面: - 构造圆方树所需时间为 $ O(N + M) $。 - 每次查询涉及一次简单的路径遍历,总时间开销为 $ O(Q \log N) $ 当采用合适的数据结构辅助时。 因此总体效率较高,适合规模输入场景下的应用需求。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值