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 = + ;
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(并查集删边)的更多相关文章
- ZOJ 3261 - Connections in Galaxy War ,并查集删边
In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ...
- 洛谷 P1197 BZOJ 1015 [JSOI2008]星球大战 (ZOJ 3261 Connections in Galaxy War)
这两道题长得差不多,都有分裂集合的操作,都是先将所有操作离线,然后从最后一步开始倒着模拟,这样一来,分裂就变成合并,也就是从打击以后最终的零散状态,一步步合并,回到最开始所有星球都被连为一个整体的状态 ...
- 题解报告:zoj 3261 Connections in Galaxy War(离线并查集)
Description In order to strengthen the defense ability, many stars in galaxy allied together and bui ...
- zoj 3261 Connections in Galaxy War(并查集逆向加边)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 题意:有很多颗星球,各自有武力值,星球间有一些联系通道,现 ...
- ZOJ 3261 Connections in Galaxy War(逆向并查集)
参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...
- ZOJ 3261 Connections in Galaxy War (逆向+带权并查集)
题意:有N个星球,每个星球有自己的武力值.星球之间有M条无向边,连通的两个点可以相互呼叫支援,前提是对方的武力值要大于自己.当武力值最大的伙伴有多个时,选择编号最小的.有Q次操作,destroy为切断 ...
- zoj 3261 Connections in Galaxy War
点击打开链接zoj 3261 思路: 带权并查集 分析: 1 题目说的是有n个星球0~n-1,每个星球都有一个战斗值.n个星球之间有一些联系,并且n个星球之间会有互相伤害 2 根本没有思路的题,看了网 ...
- ZOJ-3261 Connections in Galaxy War 并查集 离线操作
题目链接:https://cn.vjudge.net/problem/ZOJ-3261 题意 有n个星星,之间有m条边 现一边询问与x星连通的最大星的编号,一边拆开一些边 思路 一开始是真不会,甚至想 ...
- ZOJ3261 Connections in Galaxy War 并查集
分析:对于这种删边操作,我们通常可以先读进来,然后转化离线进行倒着加边 #include <stdio.h> #include <string.h> #include < ...
随机推荐
- Apache 开启压缩传输
在 /etc/httpd/conf/httpd.conf 中添加如下配置: # Enable gzip - by Jerryhuang # <IfModule mod_deflate.c> ...
- Rainbond v5.1.2发布,微服务架构应用便捷管理和交付
Rainbond v5.1.2发布,微服务架构应用便捷管理和交付 Rainbond是开源的企业应用云操作系统,支撑企业应用的开发.架构.交付和运维的全流程,通过无侵入架构,无缝衔接各类企业应用,底层资 ...
- 构造器引用和直接用new创建对象区别
万事用事实说话 package cn.lonecloud; /** * @author lonecloud * @version v1.0 * @date 上午11:22 2018/4/30 */ p ...
- Jupyter-Notebook 删除指定 kernel
原来是Python3+C# 查看列表jupyter kernelspec list 删除指定kernel:jupyter kernelspec remove icsharpkernel 删除成功:(刷 ...
- 工厂方法模式(Factory Method)和抽象工厂模式(Abstact Factory)
分类 工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的.工厂模式在<Java 与模式>中分为三类:1)简单工厂模式(Simple Facto ...
- 基于Senparc.CO2NET 缓存策略扩展的缓存使用方法
没啥说的,直接上代码 1.缓存 CacheFactory 实现: //---------------------------------------------------------------- ...
- Jmeter接口自动化
基本思路: 在excel中维护测试用例,包括访问协议,服务器名或IP,路径,请求的方法,端口号,提交参数,测试结果等,使用CSV Data Set Config读取请求信息并写入测试结果,后期只要维护 ...
- 灰度发布/AB test
背景 互联网产品有一个特点,就是不停的升级,升级,再升级.一般采用敏捷开发的团队,基本上保持每周一次的发布频率,系统升级总是伴随着风险,新旧版本兼容的风险,用户使用习惯突然改变而造成用户流失的风险,系 ...
- 第一篇-Django建立数据库各表之间的联系(上)
多表操作(一对多) 遇到的问题: 执行python manage.py makemigrations后报如下错误 TypeError: __init__() missing 1 required po ...
- Gym - 101755G Underpalindromity (树状数组)
Let us call underpalindromity of array b of length k the minimal number of times one need to increme ...