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. 让$this->error()返回json配置

    // 表单请求类型伪装变量'var_method' => '_method',// 表单ajax伪装变量'var_ajax' => '自定义',// 表单pjax伪装变量'var_pjax ...

  2. Linux block(1k) block(4k) 换算 gb

    输入 df  显示1k blocks  大小   再输入  df -h  显示 gb换算大小  结论 block(1k) 计算公式为:  block(1k)   /1024/1000  = xx gb ...

  3. 最小表示法模板(洛谷P1368 工艺)(最小表示法)

    洛谷题目传送门 最小表示是指一个字符串通过循环位移变换(第一个移到最后一个)所能得到的字典序最小的字符串. 因为是环状的,所以肯定要先转化为序列,把原串倍长. 设决策点为一个表示法的开头.比较两个决策 ...

  4. BZOJ3456 城市规划 【多项式求ln】

    题目链接 BZOJ3456 题解 真是一道经典好题,至此已经写了分治\(NTT\),多项式求逆,多项式求\(ln\)三种写法 我们发现我们要求的是大小为\(n\)无向联通图的数量 而\(n\)个点的无 ...

  5. [SCOI2007]压缩(区间dp)

    神仙题,看了半天题解才看明白... 因为题目里说如果没有m,会自动默认m在最前面. 我们设计状态为dp[l][r][0/1]为在区间l到r中有没有m的最小长度. 转移:枚举我们要压缩的起点,dp[l] ...

  6. empty() 与 html("") 的区别

    empty,首先循环给后代元素移除绑定.清除jquery给此dom的cache,然后循环removeFirstChild. 而html(''),则是简单暴力的设置innerHTML = ''; 查看文 ...

  7. yd的汇总

    因为是我这只蒟蒻个人的汇总嘛,可能有些奇♂怪的东西或者不规范的语言出现啦,见谅见谅 搬了一些到知识汇总里,删了一些过时和无用的,少了好多=.= 1.STL_queue 经实践验证,!qs.empty( ...

  8. 收藏:H.264编码原理以及I帧B帧P帧

    来源: https://www.cnblogs.com/herenzhiming/articles/5106178.html 前言 ----------------------- H264是新一代的编 ...

  9. linux系统调用之文件系统操作

    access 确定文件的可存取性 chdir 改变当前工作目录 fchdir 参见chdir chmod 改变文件方式 fchmod 参见chmod chown 改变文件的属主或用户组 fchown ...

  10. 第十五节,卷积神经网络之AlexNet网络详解(五)

    原文 ImageNet Classification with Deep ConvolutionalNeural Networks 下载地址:http://papers.nips.cc/paper/4 ...