Connections in Galaxy War——逆向并查集

题目链接

题意:

首先给定一个n 然后给你 n个数 表示 每个恒星的能量大小 p【i】 然后给定一个m 然后输入m 条恒星之间的连接关系

再给定一个q  q行 如果 query x 表示查询与 x 号恒星连接并且能量最多的恒星编号 如果 destroy x y 表示破环 x恒星与y恒星 之间的连接

题解:

传统的做法是先把输入的点加入并查集建立关系,然后开始判断询问,遇到destroy就将那两个点之间的关系断开,但是很明显,这样很难做到将其连接关系断开

正难则反

我们先把未被destroy的恒星连接起来,这样我们就可以从最后一个询问开始判断并记录下结果,当遇到destroy时再把这两个点建立关系,这样destroy前面的询问就可以正确地判断了

如何把未被destroy的恒星连接起来?

先把所有星球的连接关系存起来,然后把destroy的的恒星标记起来,没被标记的就是未被destroy的,然后 join() 一下即可

注意 恒星的编号是从 0 开始的 还要保证 输入的编号 u,v 要保证 u<v  即(有序即可)不然可能会出现,加边的时候是1,2但是删边的时候是2,1的情况

代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=1e4+5;
const int Hash=1000;
int n,m;
int p[maxn],f[maxn],ans[maxn];
int pos[maxn],maxx[maxn];
struct node
{
    int x,y;
}a[maxn];
struct edge
{
    int op,x,y;
}e[maxn];
map<int,int>vis;
char s[maxn];
int Find(int x)
{
    return f[x]==x?x:f[x]=Find(f[x]);
}
void join(int x,int y)
{
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy)
    {
        f[fx]=fy;
        if(maxx[fx]>maxx[fy])
        {
            maxx[fy]=maxx[fx];
            pos[fy]=pos[fx];
        }
        else if(maxx[fx]==maxx[fy] && pos[fx]<pos[fy])
        {
            pos[fy]=pos[fx];
        }
    }
}
int main()
{
    int flag=0;
    while(~scanf("%d",&n))
    {

        if(flag)printf("\n");
        else flag=1;

        for(int i=0;i<n;i++)
        {
            scanf("%d",&p[i]);
            maxx[i]=p[i];
            pos[i]=i;
            f[i]=i;
        }

        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
            if(a[i].x>a[i].y)swap(a[i].x,a[i].y);
        }
        int q;
        scanf("%d",&q);
        vis.clear();
        for(int i=0;i<q;i++)
        {
            scanf("%s",s);
            if(s[0]==‘q‘)
            {
                e[i].op=0;
                scanf("%d",&e[i].x);
            }
            else
            {
                e[i].op=1;
                scanf("%d%d",&e[i].x,&e[i].y);
                if(e[i].x>e[i].y)swap(e[i].x,e[i].y);
                vis[e[i].x*Hash+e[i].y]=1;
            }
        }
        for(int i=0;i<m;i++)
        {
            if(!vis[a[i].x*Hash+a[i].y])join(a[i].x,a[i].y);
        }
        int cnt=0;

        for(int i=q-1;i>=0;i--)
        {
            if(e[i].op==0)
            {
                int x=e[i].x;
                int fx=Find(x);
                if(maxx[fx]>p[x])ans[cnt++]=pos[fx];
                else ans[cnt++]=-1;
            }
            else
            {
                join(e[i].x,e[i].y);
            }
        }
        for(int i=cnt-1;i>=0;i--)printf("%d\n",ans[i]);

    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/j666/p/11609892.html

时间: 2024-10-11 22:17:39

Connections in Galaxy War——逆向并查集的相关文章

Connections in Galaxy War(逆向并查集)

Connections in Galaxy War http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563 Time Limit: 3 Seconds      Memory Limit: 32768 KB In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional

zoj 3261 Connections in Galaxy War(并查集+离线逆向操作)

 题目:给出一些点,每个点有权值,然后有一些边,相连.无向的.然后有一些操作 query a.表示从a出发的能到达的所有点权值最大的点的编号(相同取编号最小,而且权值要比自己大) destory a,b 表示删除连接a,b的边 思路并查集,但是要逆向处理,所以先离线读入,从后向前处理,于是对于destroy操作,等价于连接两个点的操作,然后对于每个询问输出即可 #include<cstdio> #include<cstring> #include<cmath> #i

ZOJ 3261 - Connections in Galaxy War ,并查集删边

In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional tunnels to exchange messages. However, when the Galaxy War began, some tunnels were destroyed by the monsters from another dimension. Then m

ZOJ - 3261 Connections in Galaxy War(并查集删边)

https://cn.vjudge.net/problem/ZOJ-3261 题意 银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可通过通道找到能量值最大的星球来帮忙.但是有一些通道被怪兽破坏了. 现在先给出原来的所有通道, 然后进行询问,询问有两种方式: destroy a b: 连接a,b的通道被怪兽破坏了 query a: 询问a能否通过通道找到救兵,只能找能量值比自己大的救兵. 分析 逆向思维,先离线存储所有的输入操作,

ZOJ 3261 Connections in Galaxy War (逆向+带权并查集)

题意:有N个星球,每个星球有自己的武力值.星球之间有M条无向边,连通的两个点可以相互呼叫支援,前提是对方的武力值要大于自己.当武力值最大的伙伴有多个时,选择编号最小的.有Q次操作,destroy为切断连接两点的边,query为查询某星球能不能向它人呼叫支援. 还是需要离线逆向并查集求解.思路和HDU 4496很相似,但是此处不一定是把所有边都删去,所以需要删边的情况建立出最终的状态.因为N可以到1e4,所以可以用map嵌套map的方式记录过程中被删去的边,最后再根据删除情况建立状态. 在合并时需

ZOJ 3261 Connections in Galaxy War (并查集)

Connections in Galaxy War Time Limit: 3 Seconds      Memory Limit: 32768 KB In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional tunnels to exchange messages. However, when the Galaxy War began

UVA 10158 War(并查集)

War A war is being lead between two countries, A and B. As a loyal citizen of C, you decide to help your country's espionage by attending the peace-talks taking place these days (incognito, of course). There are n people at the talks (not including y

题解报告:zoj 3261 Connections in Galaxy War(离线并查集)

Description In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional tunnels to exchange messages. However, when the Galaxy War began, some tunnels were destroyed by the monsters from another dimen

hdu-2874 Connections between cities(lca+tarjan+并查集)

题目链接: Connections between cities Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, s