【刷题】洛谷 P3950 部落冲突

题目背景

在一个叫做Travian的世界里,生活着各个大大小小的部落。其中最为强大的是罗马、高卢和日耳曼。他们之间为了争夺资源和土地,进行了无数次的战斗。期间诞生了众多家喻户晓的英雄人物,也留下了许多可歌可泣的动人故事。

1456415099616.jpg

其中,在大大小小的部落之间,会有一些道路相连,这些道路是Travian世界里的重要枢纽,简单起见,你可以把这些部落与部落之间相连的道路看作一颗树,可见每条道路对于Travian世界的重要程度。有了这些道路,建筑工人就可以通过这些道路进行友好外交啦。

然而,事情并不会像想象的那样美好,由于资源的匮乏,相邻的部落(由一条道路相连的部落)之间经常会发生大大小小的冲突事件,更有甚者,会升级为部落之间的大型战争。

为了避免误伤,每当两个相邻的部落之间发生大型战争之时,这两个部落间的道路是不允许通行的,对于一些强大的部落,甚至能与多个相邻的部落同时开战,同样的,这些战争地带的道路十分危险,是不可通行的。

天下之势,分久必合,当两个部落经历了不打不相识的苦战之后,他们可以签订停战协议(暂时停战,以后依旧可能再次开战),这样,两个部落之间的道路又会重新恢复为可通行状态,建筑工人们又可以经过此地购买最新的大本营设计图纸来强大自己的部落了。

为了简单起见,我们把各大战争事件按发起的时间顺序依次编号(最先发起的战争编号就为 1,第二次战争编号就为 2,以此类推),当两个部落停战之时,则会直接告诉你这场战争的编号,然后这场战争就载入了史册,不复存在了,当然,这并不会影响到其他战争的编号。

建筑工人十分讨厌战争,因为战争,想从一个部落到另一个部落进行友好交流的建筑工人可能就此白跑一趟。所以,在他们出发之前,都会向你问问能不能到达他们想去的部落。

题目描述

简单起见,你就是要处理下面三件事,所有的事件都是按照时间顺序给出的。

  1. \(Q \ p \ q\) 从第 \(p\) 个部落出发的建筑工人想知道能否到达第 \(q\) 个部落了,你要回答的便是 \(Yes/No\),注意大小写

  2. \(C \ p \ q\)\(p\) 个部落与第 \(q\) 个部落开战了,保证他们一定是相邻的部落,且目前处于停战(未开战)状态

  3. \(U \ x\)\(x\) 次发生的战争结束了,它将永远的被载入史册,不复存在(保证这个消息不会告诉你多次)

输入输出格式

输入格式:

第一行两个数 \(n\)\(m\)\(n\) 代表了一共有 \(n\) 个部落,\(m\) 代表了以上三种事件发生的总数

接下来的 \(n - 1\) 行,每行两个数 \(p\) , \(q\) ,代表了第 \(p\) 个部落与第 \(q\) 个部落之间有一条道路相连

接下来的 \(m\) 行,每行表示一件事,详见题目描述

输出格式:

每行一个“ \(Yes\) ”或者“ \(No\) ”,表示从第 \(p\) 个部落出发的建筑工人能否到达第 \(q\) 个部落

输入输出样例

输入样例#1:

5 9
1 2
2 3
3 4
4 5
Q 1 4
C 2 1
C 4 3
Q 3 1
Q 1 5
U 1
U 2
C 4 3
Q 3 4

输出样例#1:

Yes
No
No
No

输入样例#2:

10 10
1 2
1 3
3 4
3 5
1 6
3 7
1 8
2 9
5 10
C 8 1
Q 6 1
C 2 1
Q 2 10
U 1
C 9 2
C 7 3
U 3
Q 6 7
Q 1 10

输出样例#2:

Yes
No
No
Yes

输入样例#3:

20 20
1 2
1 3
2 4
1 5
1 6
4 7
1 8
2 9
5 10
1 11
2 12
7 13
1 14
1 15
11 16
4 17
3 18
18 19
8 20
Q 13 5
C 14 1
C 16 11
U 1
U 2
C 20 8
Q 7 1
C 7 4
Q 17 17
Q 1 6
C 16 11
C 2 1
Q 16 2
U 3
U 5
U 6
C 2 1
C 6 1
C 13 7
C 11 1

输出样例#3:

Yes
Yes
Yes
Yes
No

说明

对于30%的数据 1<=n,m<=6000

对于另30%的数据,保证部落之间的地理关系是一条链,且 i 与 i + 1 之间有一条道路

对于另30%的数据,1<=n,m<=100000

对于100%的数据,1<=n,m<=300000

题解

LCT
化边为点之后,裸的维护连通性的题目

#include<bits/stdc++.h>
#define ll long long
#define db double
#define ld long double
const int MAXN=300000+10,MAXM=300000+10;
int n,m,wnt;
struct war{
    int u,v;
};
war W[MAXM];
std::map<int,int> M[MAXN];
#define lc(x) ch[(x)][0]
#define rc(x) ch[(x)][1]
struct LCT{
    int ch[MAXN+MAXN][2],fa[MAXN+MAXM],rev[MAXN+MAXM],stack[MAXN+MAXM],cnt;
    inline void init()
    {
        memset(ch,0,sizeof(ch));
        memset(fa,0,sizeof(fa));
        memset(rev,0,sizeof(rev));
    }
    inline bool nroot(int x)
    {
        return lc(fa[x])==x||rc(fa[x])==x;
    }
    inline void reverse(int x)
    {
        std::swap(lc(x),rc(x));
        rev[x]^=1;
    }
    inline void pushdown(int x)
    {
        if(rev[x])
        {
            if(lc(x))reverse(lc(x));
            if(rc(x))reverse(rc(x));
            rev[x]=0;
        }
    }
    inline void rotate(int x)
    {
        int f=fa[x],p=fa[f],c=(rc(f)==x);
        if(nroot(f))ch[p][rc(p)==f]=x;
        fa[ch[f][c]=ch[x][c^1]]=f;
        fa[ch[x][c^1]=f]=x;
        fa[x]=p;
    }
    inline void splay(int x)
    {
        cnt=0;
        stack[++cnt]=x;
        for(register int i=x;nroot(i);i=fa[i])stack[++cnt]=fa[i];
        while(cnt)pushdown(stack[cnt--]);
        for(register int y=fa[x];nroot(x);rotate(x),y=fa[x])
            if(nroot(y))rotate((lc(y)==x)==(lc(fa[y])==y)?y:x);
    }
    inline void access(int x)
    {
        for(register int y=0;x;x=fa[y=x])splay(x),rc(x)=y;
    }
    inline int findroot(int x)
    {
        access(x);splay(x);
        while(lc(x))pushdown(x),x=lc(x);
        splay(x);
        return x;
    }
    inline void makeroot(int x)
    {
        access(x);splay(x);reverse(x);
    }
    inline void split(int x,int y)
    {
        makeroot(x);access(y);splay(y);
    }
    inline void link(int x,int y)
    {
        makeroot(x);
        if(findroot(y)!=x)fa[x]=y;
    }
    inline void cut(int x,int y)
    {
        makeroot(x);
        if(findroot(y)==x&&fa[y]==x&&!rc(y))fa[y]=lc(x)=0;
    }
};
LCT T;
#undef lc
#undef rc
template<typename T> inline void read(T &x)
{
    T data=0,w=1;
    char ch=0;
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    x=data*w;
}
template<typename T> inline void write(T x,char c='\0')
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
    if(c!='\0')putchar(c);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
int main()
{
    read(n);read(m);
    T.init();
    for(register int i=1;i<n;++i)
    {
        int u,v,sn=n+i;
        read(u);read(v);
        if(u>v)std::swap(u,v);
        M[u][v]=i;
        T.link(sn,u);T.link(sn,v);
    }
    while(m--)
    {
        char opt;
        std::cin>>opt;
        if(opt=='Q')
        {
            int u,v;
            read(u);read(v);
            if(T.findroot(u)==T.findroot(v))puts("Yes");
            else puts("No");
        }
        if(opt=='C')
        {
            ++wnt;
            read(W[wnt].u);read(W[wnt].v);
            if(W[wnt].u>W[wnt].v)std::swap(W[wnt].u,W[wnt].v);
            int sn=n+M[W[wnt].u][W[wnt].v];
            T.cut(sn,W[wnt].u);T.cut(sn,W[wnt].v);
        }
        if(opt=='U')
        {
            int id,sn;
            read(id);
            sn=n+M[W[id].u][W[id].v];
            T.link(sn,W[id].u);T.link(sn,W[id].v);
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/hongyj/p/8714685.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值