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 = + ;
const int maxm = + ;
const int mod = ;
int fa[maxn];
int n,m;
struct ND{
char op[];
int a,b;
};
vector<ND> ask;
int HASH = maxn;
vector<pair<int,int> >edge;
int w[maxn];
int ans[];
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=;i<n;i++) scanf("%d",&w[i]);
for(int i=;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[]=='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=;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=;
for(int i=q-;i>=;i--){
if(ask[i].op[]=='q'){
int f=find(ask[i].a);
if(w[f]>w[ask[i].a]) ans[cnt++]=f;
else ans[cnt++]=-;
}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-;i>=;i--) printf("%d\n",ans[i]); }
return ;
}

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

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

    In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ...

  2. 洛谷 P1197 BZOJ 1015 [JSOI2008]星球大战 (ZOJ 3261 Connections in Galaxy War)

    这两道题长得差不多,都有分裂集合的操作,都是先将所有操作离线,然后从最后一步开始倒着模拟,这样一来,分裂就变成合并,也就是从打击以后最终的零散状态,一步步合并,回到最开始所有星球都被连为一个整体的状态 ...

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

    Description In order to strengthen the defense ability, many stars in galaxy allied together and bui ...

  4. zoj 3261 Connections in Galaxy War(并查集逆向加边)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 题意:有很多颗星球,各自有武力值,星球间有一些联系通道,现 ...

  5. ZOJ 3261 Connections in Galaxy War(逆向并查集)

    参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...

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

    题意:有N个星球,每个星球有自己的武力值.星球之间有M条无向边,连通的两个点可以相互呼叫支援,前提是对方的武力值要大于自己.当武力值最大的伙伴有多个时,选择编号最小的.有Q次操作,destroy为切断 ...

  7. zoj 3261 Connections in Galaxy War

    点击打开链接zoj 3261 思路: 带权并查集 分析: 1 题目说的是有n个星球0~n-1,每个星球都有一个战斗值.n个星球之间有一些联系,并且n个星球之间会有互相伤害 2 根本没有思路的题,看了网 ...

  8. ZOJ-3261 Connections in Galaxy War 并查集 离线操作

    题目链接:https://cn.vjudge.net/problem/ZOJ-3261 题意 有n个星星,之间有m条边 现一边询问与x星连通的最大星的编号,一边拆开一些边 思路 一开始是真不会,甚至想 ...

  9. ZOJ3261 Connections in Galaxy War 并查集

    分析:对于这种删边操作,我们通常可以先读进来,然后转化离线进行倒着加边 #include <stdio.h> #include <string.h> #include < ...

随机推荐

  1. 【XSY1515】【GDKOI2016】小学生数学题 组合数学

    题目描述 给你\(n,k,p\)(\(p\)为质数),求 \[ \sum_{i=1}^n\frac{1}{i}\mod p^k \] 保证有解. \(p\leq {10}^5,np^k\leq {10 ...

  2. hdu 2844 Coins (多重背包+二进制优化)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 思路:多重背包 , dp[i] ,容量为i的背包最多能凑到多少容量,如果dp[i] = i,那么代表 ...

  3. Install KVM Hypervisor on arrch64 Linux Server

    Install KVM Hypervisor on arrch64 Linux Server 参考链接: https://wiki.ubuntu.com/ARM64/QEMU https://wiki ...

  4. Android studio preview界面无法预览,报错render problem

    1.查看报错信息,如果有报错,该叹号应为红色,点击查看报错,显示为render problem 2.打开res/styles.xml修改为如图,添加Base. 3.再打开preview界面

  5. 【Gym - 100947G】Square Spiral Search

    BUPT 2017 summer training (for 16) #1C 题意 A new computer scientist is trying to develop a new memory ...

  6. 线性基求第k小异或值

    题目链接 题意:给由 n 个数组成的一个可重集 S,每次给定一个数 k,求一个集合 \(T \subseteq S\), 使得集合 T 在 S 的所有非空子集的不同的异或和中, 其异或和 \(T_1 ...

  7. Netty如何实现Reactor模式

    在前面的文章中(Reactor模型详解),我们讲解了Reactor模式的各种演变形式,本文主要讲解的则是Netty是如何实现Reactor模式的.这里关于Netty实现的Reactor模式,需要说明的 ...

  8. HZNU ACM一日游 2019.3.17 【2,4,6-三硝基甲苯(TNT)】

    Travel Diary 早上8:00到HG,听说hjc20032003在等我. 然后他竟然鸽我...最后还是勉强在8:30坐上去偏僻的HZNU的地铁. 到文新,然后带上fjl,打滴滴,一行人来到了H ...

  9. Nifi 模板

        Acqusition_and_Processing.xml Moving templates to own directory to make repo cleaner   CADF_Pars ...

  10. 「SCOI2015」小凸玩矩阵 解题报告

    「SCOI2015」小凸玩矩阵 我好沙茶啊 把点当边连接行和列,在外面二分答案跑图的匹配就行了 我最开始二分方向搞反了,样例没过. 脑袋一抽,这绝壁要费用流,连忙打了个KM 然后wa了,一想这个不是完 ...