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

https://cn.vjudge.net/problem/ZOJ-3261

题意

银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可通过通道找到能量值最大的星球来帮忙。但是有一些通道被怪兽破坏了。

现在先给出原来的所有通道, 然后进行询问,询问有两种方式:

destroy a b: 连接a,b的通道被怪兽破坏了

query a: 询问a能否通过通道找到救兵,只能找能量值比自己大的救兵。

分析

逆向思维,先离线存储所有的输入操作,然后把被完全破坏之后的通道连接起来,然后再从最后一个询问往前面推,当有destroy a b时就把a,b再Union起来。

这里使用哈希思想来判断一条边是否被破坏了,注意格式。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = 10000 + 10;
const int maxm = 200000 + 10;
const int mod = 998244353;
int fa[maxn];
int n,m;
struct ND{
    char op[10];
    int a,b;
};
vector<ND> ask;
int HASH = maxn;
vector<pair<int,int> >edge;
int w[maxn];
int ans[50004];
map<int,bool> ma;
int find(int x){
    return x==fa[x]?x:fa[x]=find(fa[x]);
}
void Union(int x,int y){
    int fx=find(x),fy=find(y);
    if(fx!=fy){
        if(w[fx]>w[fy]){
            fa[fy]=fx;
        }else if(w[fx]<w[fy]){
            fa[fx]=fy;
        }else{
            if(fx>fy){
                fa[fx]=fy;
            }else{
                fa[fy]=fx;
            }
        }
    }
}

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    bool flag=false;
    while(~scanf("%d",&n)){
        for(int i=0;i<n;i++) scanf("%d",&w[i]);
        for(int i=0;i<=n;i++) fa[i]=i;
        edge.clear();
        ask.clear();
        ma.clear();
        scanf("%d",&m);
        while(m--){
            int x,y;
            scanf("%d%d",&x,&y);
            if(x>y) swap(x,y);
            edge.push_back(make_pair(x,y));
        }
        int q;
        scanf("%d",&q);
        while(q--){
            ND tmp;
            scanf("%s",tmp.op);
            if(tmp.op[0]==‘q‘){
                scanf("%d",&tmp.a);
            }else{
                scanf("%d%d",&tmp.a,&tmp.b);
                if(tmp.a>tmp.b) swap(tmp.a,tmp.b);
                ma[tmp.a*HASH+tmp.b]=true;
            }
            ask.push_back(tmp);
        }
        q=edge.size();
        for(int i=0;i<q;i++){
            if(ma[edge[i].first*HASH+edge[i].second]) continue;
            Union(edge[i].first,edge[i].second);
        }
//        for(int i=0;i<n;i++) printf("%d ",fa[i]);puts("");
        q=ask.size();
        int cnt=0;
        for(int i=q-1;i>=0;i--){
            if(ask[i].op[0]==‘q‘){
                int f=find(ask[i].a);
                if(w[f]>w[ask[i].a]) ans[cnt++]=f;
                else ans[cnt++]=-1;
            }else{
//                cout<<ask[i].a<<‘ ‘<<ask[i].b<<endl;
                Union(ask[i].a,ask[i].b);
//                for(int i=0;i<n;i++) printf("%d ",fa[i]);puts("");
            }
        }
        if(flag) puts("");
        flag=true;
        for(int i=cnt-1;i>=0;i--) printf("%d\n",ans[i]);

    }
    return 0;
}

原文地址:https://www.cnblogs.com/fht-litost/p/9568804.html

时间: 2024-10-06 12:39:00

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

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

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有直接或间接通路的星

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