ZOJ-3261-Connections in Galaxy War [逆向并查集]


题目传送门


题意:
每个星星都有自己的power,星星之间有隧道可以联通,星星只能向比自己power更多的星星求救,如果最大的power相同就向序号更小的星星求救。
有两种操作:
“query x” 询问x可以求救的星星序号
“destroy x y” 破坏x - y连接的通道
思路:
如果直接对并查集进行删除操作会很麻烦,所以就将所有操作全部存起来逆向操作,先将所有没有被破坏的通道合并,然后逆向询问,破坏即为建边合并。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
int N, M, Q;
int val[12000];
map<int, map<int,int> >mark;
struct node1{
    int x,y;
}P[22000];
struct node2{
    char ch[10];
    int x,y;
}K[50000+1000];
int fa[12000], ans[50000+1000];
void init()
{
    for (int i = 0; i < 10000; i++)
    {
        fa[i]=i;
    }
    return ;
}
int find(int x)
{
    while (x!=fa[x]) x = fa[x];
    return x;
}
void unite(int x, int y)
{
    int a = find(x);
    int b = find(y);
    if (a!=b)
    {
        if (val[a]==val[b])
        {
            if (a<b)
                fa[b] = a;
            else
                fa[a] = b;
        }
        else
        {
            if (val[a]>val[b])
                fa[b] = a;
            else
                fa[a] = b;
        }
    }
}
int main(void)
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    bool flag = false;
    while (~scanf("%d", &N))
    {
        init();
        for (int i = 0; i < N; i++)
            scanf("%d", &val[i]);
        scanf("%d", &M);
        for (int i = 0; i < M; i++)
            scanf("%d %d", &P[i].x, &P[i].y);
        scanf("%d", &Q);
        for (int i = 0; i < Q; i++)
        {
            scanf(" %s", K[i].ch);
            if (K[i].ch[0]=='q')
                scanf("%d", &K[i].x);
            else
            {
                scanf("%d %d", &K[i].x, &K[i].y);
                mark[K[i].x][K[i].y] = mark[K[i].y][K[i].x] = 1;
            }
        }
        for (int i = 0; i < M; i++)
        {
            if (mark[P[i].x][P[i].y]==1)
                continue;
            unite(P[i].x, P[i].y);
        }
        int cnt = 0;
        for (int i = Q-1; i>=0; i--)
        {
            if (K[i].ch[0]=='q')
            {
                int a = find(K[i].x);
                if (a==K[i].x)
                    ans[cnt++] = -1;
                else
                {
                    if (val[a]>val[K[i].x])
                        ans[cnt++]=a;
                    else
                        ans[cnt++]=-1;
                }
            }
            else
            {
                unite(K[i].x, K[i].y);
            }
        }
        if (flag)
            printf("\n");
        else
            flag = true;
        for (int i = cnt-1; i>=0; i--)
            printf("%d\n", ans[i]);
        mark.clear();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值