【SDOI2008】【BZOJ2049】Cave 洞穴勘测

【题目链接】

【前置技能】

  • LCT

【题解】

  • LCT模板题,支持link,cut,以及判断树上两点间的连通性。
  • 时间复杂度 O ( Q l o g N ) O(QlogN) O(QlogN)

【代码】

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL  long long
#define MAXN    10010
using namespace std;
int n, m;
char s[20];
 
template <typename T> void chkmin(T &x, T y){x = min(x, y);}
template <typename T> void chkmax(T &x, T y){x = max(x, y);}
template <typename T> void read(T &x){
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}
 
struct Link_Cut_Tree{
    struct info{int son[2], up, fa, tag;}a[MAXN];
    int cnt;
    int get(int pos){
        return (pos == a[a[pos].fa].son[1]);
    }
    void push_down(int pos){
        if (!a[pos].tag) return;
        swap(a[pos].son[0], a[pos].son[1]);
        if (a[pos].son[0]) a[a[pos].son[0]].tag ^= 1;
        if (a[pos].son[1]) a[a[pos].son[1]].tag ^= 1;
        a[pos].tag = 0;
    }
    void rotate(int pos){
        int fa = a[pos].fa, gr = a[fa].fa;
        a[pos].up = a[fa].up, a[fa].up = 0;
        push_down(fa), push_down(pos);
        int x = get(pos), f = get(fa);
        if (a[pos].son[x ^ 1]) a[a[pos].son[x ^ 1]].fa = fa;
        a[fa].son[x] = a[pos].son[x ^ 1];
        a[fa].fa = pos;
        a[pos].son[x ^ 1] = fa;
        if (gr) a[gr].son[f] = pos;
        a[pos].fa = gr;
    }
    void splay(int pos){
        push_down(pos);
        for (int fa = a[pos].fa; (fa = a[pos].fa) != 0; rotate(pos))
            if (a[fa].fa != 0) {
                if (get(pos) == get(fa)) rotate(fa);
                else rotate(pos);
            }
    }
    void access(int x){
        splay(x);
        int tmp = a[x].son[1];
        if (tmp) a[tmp].fa = 0, a[tmp].up = x;
        a[x].son[1] = 0;
        while (a[x].up){
            int f = a[x].up;
            splay(f);
            int tmp = a[f].son[1];
            if (tmp) a[tmp].fa = 0, a[tmp].up = f;
            a[f].son[1] = x, a[x].up = 0, a[x].fa = f;
            splay(x);
        }
    }
    void reverse(int x){
        access(x);
        a[x].tag ^= 1;
        push_down(x);
    }
    void link(int x, int y){
        access(x), reverse(y);
        a[x].son[1] = y;
        a[y].fa = x;
    }
    void cut(int x, int y){
        reverse(x), access(y), splay(x);
        a[x].son[1] = 0, a[y].fa = 0;
    }
    int findroot(int x){
        access(x);
        push_down(x);
        while (a[x].son[0]) {
            x = a[x].son[0];
            push_down(x);
        }
        splay(x);
        return x;
    }
}lct;
 
int main(){
    read(n), read(m);
    while (m--){
        scanf("%s", s);
        int u, v;
        read(u), read(v);
        if (s[0] == 'Q') {
            if (lct.findroot(u) == lct.findroot(v)) printf("Yes\n");
            else printf("No\n");
        } else if (s[0] == 'D') {
            lct.cut(u, v);
        } else {
            lct.link(u, v);
        }
    } 
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 有一个 $n$ 个点的棋盘,每个点上有一个数字 $a_i$,你需要从 $(1,1)$ 走到 $(n,n)$,每次只能往右或往下走,每个格子只能经过一次,路径上的数字和为 $S$。定义一个点 $(x,y)$ 的权值为 $a_x+a_y$,求所有满足条件的路径中,所有点的权值和的最小值。 输入格式 第一行一个整数 $n$。 接下来 $n$ 行,每行 $n$ 个整数,表示棋盘上每个点的数字。 输出格式 输出一个整数,表示所有满足条件的路径中,所有点的权值和的最小值。 数据范围 $1\leq n\leq 300$ 输入样例 3 1 2 3 4 5 6 7 8 9 输出样例 25 算法1 (树形dp) $O(n^3)$ 我们可以先将所有点的权值求出来,然后将其看作是一个有权值的图,问题就转化为了在这个图中求从 $(1,1)$ 到 $(n,n)$ 的所有路径中,所有点的权值和的最小值。 我们可以使用树形dp来解决这个问题,具体来说,我们可以将这个图看作是一棵树,每个点的父节点是它的前驱或者后继,然后我们从根节点开始,依次向下遍历,对于每个节点,我们可以考虑它的两个儿子,如果它的两个儿子都被遍历过了,那么我们就可以计算出从它的左儿子到它的右儿子的路径中,所有点的权值和的最小值,然后再将这个值加上当前节点的权值,就可以得到从根节点到当前节点的路径中,所有点的权值和的最小值。 时间复杂度 树形dp的时间复杂度是 $O(n^3)$。 C++ 代码 算法2 (动态规划) $O(n^3)$ 我们可以使用动态规划来解决这个问题,具体来说,我们可以定义 $f(i,j,s)$ 表示从 $(1,1)$ 到 $(i,j)$ 的所有路径中,所有点的权值和为 $s$ 的最小值,那么我们就可以得到如下的状态转移方程: $$ f(i,j,s)=\min\{f(i-1,j,s-a_{i,j}),f(i,j-1,s-a_{i,j})\} $$ 其中 $a_{i,j}$ 表示点 $(i,j)$ 的权值。 时间复杂度 动态规划的时间复杂度是 $O(n^3)$。 C++ 代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值