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;
}

时间: 2024-10-18 07:59:39

ZOJ 3261 Connections in Galaxy War (并查集)的相关文章

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(离线并查集)

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

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 (逆向+带权并查集)

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

ZOJ 3261 Connections in Galaxy War

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 题意: 一. 给定N个星球(0,...,N-1)及其对应的power(p0,...pn-1) 二. 给定M条星球a与b间(a!=b)的通路 三. 给出Q个指令,"destroy a b"代表此时星球a b间的通路(必定已连接)被破坏了,"query a"要求输出当时应对星球a进行救援的星球的编号 规则为: ①与星球a有直接或间接通路的星

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

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

ZOJ3261 Connections in Galaxy War 并查集

分析:对于这种删边操作,我们通常可以先读进来,然后转化离线进行倒着加边 #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; typedef pair<int,int>pii; const int N=1e4+5; pii p[N<<1]; int v[N],fa[N],n,m,q; bo

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 3659 Conquer a New Region 并查集+贪心

点击打开链接题目链接 Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history rolling forward, our king conquered a new region in a distant continent. There are N towns (numbered from 1 to N) in this region connected by s