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, some tunnels were destroyed by the monsters from another dimension. Then many problems were raised when some of the stars wanted to seek help from the others.

In the galaxy, the stars are numbered from 0 to N-1 and their power was marked by a non-negative integer pi. When the star A wanted to seek help, it would send the message to the star with the largest power which was connected with star A directly or indirectly. In addition, this star should be more powerful than the star A. If there were more than one star which had the same largest power, then the one with the smallest serial number was chosen. And therefore, sometimes star A couldn't find such star for help.

Given the information of the war and the queries about some particular stars, for each query, please find out whether this star could seek another star for help and which star should be chosen.

Input

There are no more than 20 cases. Process to the end of file.

For each cases, the first line contains an integer N (1 <= N <= 10000), which is the number of stars. The second line contains N integers p0p1, ... , pn-1 (0 <= pi <= 1000000000), representing the power of the i-th star. Then the third line is a single integer M (0 <= M <= 20000), that is the number of tunnels built before the war. Then M lines follows. Each line has two integers ab (0 <= ab <= N - 1, a != b), which means star a and star b has a connection tunnel. It's guaranteed that each connection will only be described once.

In the (M + 2)-th line is an integer Q (0 <= Q <= 50000) which is the number of the information and queries. In the following Q lines, each line will be written in one of next two formats.

"destroy a b" - the connection between star a and star b was destroyed by the monsters. It's guaranteed that the connection between star a and star b was available before the monsters' attack.

"query a" - star a wanted to know which star it should turn to for help

There is a blank line between consecutive cases.

Output

For each query in the input, if there is no star that star a can turn to for help, then output "-1"; otherwise, output the serial number of the chosen star.

Print a blank line between consecutive cases.

Sample Input

2
10 20
1
0 1
5
query 0
query 1
destroy 0 1
query 0
query 1

Sample Output

1
-1
-1
-1



题意:有N星球编号0~N-1,每个星球有一个战斗力,在星球大战中,弱小的星球可以向其他比它更强的星球求助,当然这些星球要求直接或者间接和它相连,给出M组两个星球之间的联系,两个星球之间的通信可能会被怪兽损坏。之后给出Q组询问,query a表示编号为a的星球向其他更强的星球求助,找到与a直接或者间接相连的战斗力最大的星球,如果有多个,输出编号最小的那个,destroy a b表示a和b之间的通信被损坏。

分析:并查集,把所有数据都保存下来,然后倒着处理。对于提出的问题:及时回答,保存答案。对于删去的树枝:做并查集的合并处理。

如果按照顺序处理,先合并有联系的点,当后面有destory操作的时候,不好处理。所以就倒着处理,离线输入数据,从最后的状态开始,如果是query的时候就做查询操作,如果是destory就做合并操作。合并的时候比较当前节点的值和根节点的值的大小,更新最大值。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<string>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<stdlib.h>
#include<algorithm>
#define LL __int64
using namespace std;
const int MAXN=10000+5;
const int HASH=10000;
int n,m,Q;
int ans[50000+5];
int p[MAXN];
int val[MAXN];
map<int,int> mat;
struct EDGE
{
    int u,v;
}a[20000+5];
struct NODE
{
    char str[10];
    int u,v;
}b[50000+5];


void init()
{
    for(int i=0;i<n;i++) p[i]=i;
    memset(ans,0,sizeof(ans));
    memset(val,0,sizeof(val));
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    mat.clear();
}
int findfa(int x)
{
    return p[x]==x?x:p[x]=findfa(p[x]);
}
void Union(int u,int v)
{
    int x=findfa(u);
    int y=findfa(v);
    if(x!=y)
    {
        if(val[x]>val[y]) p[y]=x;
        else if(val[x]<val[y]) p[x]=y;
        else
        {
            if(x<y) p[y]=x;
            else p[x]=y;
        }
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    bool flag=false;
    while(scanf("%d",&n)!=EOF)
    {
        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",&a[i].u,&a[i].v);
            if(a[i].u>a[i].v) swap(a[i].u,a[i].v);
        }
        scanf("%d",&Q);
        for(int i=0;i<Q;i++)
        {
            scanf("%s",&b[i].str);
            if(b[i].str[0]=='q') scanf("%d",&b[i].u);
            else
            {
                scanf("%d %d",&b[i].u,&b[i].v);
                if(b[i].u>b[i].v) swap(b[i].u,b[i].v);
                mat[b[i].u*HASH+b[i].v]=1;
            }
        }
        for(int i=0;i<m;i++)
            if(mat[a[i].u*HASH+a[i].v]!=1)
                Union(a[i].u,a[i].v);

        int cnt=0;
        for(int i=Q-1;i>=0;i--)
        {
            if(b[i].str[0]=='q')
            {
                int x=findfa(b[i].u);
                if(val[x]>val[b[i].u]) ans[cnt++]=x;
                else ans[cnt++]=-1;
            }
            else Union(b[i].u,b[i].v);
        }

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

        for(int i=cnt-1;i>=0;i--)
            printf("%d\n",ans[i]);
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/clliff/p/4701258.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值