题意:

1.有n个星球,每个星球有一个编号(1-n)和一个能量值。

2.一开始将某些星球连通。

3.开战后有很多个操作,查询某个星球能找谁求救或者摧毁两颗星球之间的连通路径,使其不能连通。如果连通则可以相互求救,求救的对象要求能量比自己大并且在连通的星球中能量最大,如果能量最大的星球有多个,则找编号小的。

解题:

1.用并查集连通星球,把能量大的作为根节点,如果能量相同则把编号小的作为根节点,方便查询求救对象。

2.并查集没有断开的操作,把末态作为起始状态逆推。保存一开始的连通路径和开战后摧毁的连通路径,战争结束的状态作为起始状态,不连通被摧毁的连通路径。

 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std; int n,m,x,y,q;
int par[];///父亲
map<int,bool>mp;
int ans[];
struct star
{
int id;///编号
int val;///能量值
};star s[]; struct edge
{
int x;
int y;
};edge e[]; struct query
{
char s[];
int x,y; };query que[]; int find(int a)
{
if( par[a]==a )
return a;
return par[a]=find(par[a]);
} void unit(int a,int b)
{
int aa=find(a);
int bb=find(b);
if(aa!=bb)
{
if( s[aa].val==s[bb].val )///相同能量值
{
if( s[aa].id<s[bb].id )///编号小的做根节点
par[bb]=aa;
else
par[aa]=bb;
}
else if( s[aa].val<s[bb].val )///能量值大的作为根节点
par[aa]=bb;
else
par[bb]=aa;
}
} int main()
{
bool first=true;
while(scanf("%d",&n)!=EOF)///行星数量
{
if(first)///第一次进来变为false,以后每次都要打印空行
first=false;
else printf("\n");
mp.clear();
memset(ans,inf,sizeof(ans));
for(int i=;i<n;i++)///能量值
{
scanf("%d",&s[i].val);
s[i].id=i;
par[i]=i;
}
scanf("%d",&m);///连通边
for(int i=;i<m;i++)
{
scanf("%d%d",&x,&y);
if(x>y)///保证x<y
swap(x,y);
e[i].x=x;
e[i].y=y;///存连通边
mp[ x*+y ]=false;///没被毁掉
}
scanf("%d",&q);///查询
for(int i=;i<q;i++)
{
getchar();
scanf("%s",que[i].s);
if(que[i].s[]=='q')
scanf("%d",&que[i].x);
else
{
scanf("%d %d",&x,&y);
if(x>y)///保证x<y
swap(x,y);
que[i].x=x;
que[i].y=y;
mp[ x*+y ]=true;///被毁掉
}
}
///离线处理
for(int i=;i<m;i++)///把没被毁掉的边的末态作为起始态,逆推
{
x=e[i].x;
y=e[i].y;
if( !mp[ x*+y ] )
unit(x,y);
}
///逆推,保存对应的答案
for(int i=q-;i>=;i--)
{
if( que[i].s[]=='q' )
{
x=find(que[i].x);///连通星球中 能量最大的 根星球
if( s[x].val>s[ que[i].x ].val )
ans[i]=s[x].id;
else
ans[i]=-;
}
else
unit( que[i].x,que[i].y );
}
///顺序输出答案
for(int i=;i<q;i++)
{
if(ans[i]!=inf)
printf("%d\n",ans[i]);
}
}
return ;
}

ZOJ3261-Connections in Galaxy War-(逆向并查集+离线处理)的更多相关文章

  1. ZOJ3261:Connections in Galaxy War(逆向并查集)

    Connections in Galaxy War Time Limit: 3 Seconds      Memory Limit: 32768 KB 题目链接:http://acm.zju.edu. ...

  2. Connections in Galaxy War (逆向并查集)题解

    Connections in Galaxy War In order to strengthen the defense ability, many stars in galaxy allied to ...

  3. Connections in Galaxy War(逆向并查集)

    Connections in Galaxy War http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563 Time Limit ...

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

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

  5. ZOJ3261 Connections in Galaxy War —— 反向并查集

    题目链接:https://vjudge.net/problem/ZOJ-3261 In order to strengthen the defense ability, many stars in g ...

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

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

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

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

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

    https://cn.vjudge.net/problem/ZOJ-3261 题意 银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可 ...

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

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

随机推荐

  1. Matlab 整数线性规划问题模型代码

    整数线性规划问题的基本内容 整数线性规划解决的是自变量在一定的线性约束条件下,使得线性目标函数求得最大值或者最小值的问题.其中自变量只能取整数.特别地,当自变量只能取0或者1时,称之为 0-1 整数规 ...

  2. Zookeeper简单入门

    官网地址https://zookeeper.apache.org/releases.html 下载https://mirrors.tuna.tsinghua.edu.cn/apache/zookeep ...

  3. Github 上的个人项目开源心得

    原文链接 https://elfgzp.cn/2019/12/09/gortal-site-project 由于最近在 Github 发了一个个人开源项目 - 「gortal」一个使用 Go 语言开发 ...

  4. IDEA——配置代码检测

    一.问题 利用idea安装代码检查机制,完成bug的检测.内容分为两部分:1.完成工具的安装2.完成代码的审核 二.解决1.工具的安装由于idea这个ide具有的性质,所以具有两种不同的安装方式.1) ...

  5. map集合转set集合

    import java.util.*; //Map集合的迭代器输出,先将Map集合变为Set集合,再使用Iterator迭代器 public class Java_collection { publi ...

  6. 解决Linq Join Group by 时报错:Nullable object must have a value.

    Linq Join Group by 时报Nullable object must have a value. 例如: from s in subject on ch.SubId equals s.S ...

  7. Angular复习笔记6-依赖注入

    Angular复习笔记6-依赖注入 依赖注入(DependencyInjection)是Angular实现重要功能的一种设计模式.一个大型应用的开发通常会涉及很多组件和服务,这些组件和服务之间有着错综 ...

  8. [K8s 1.9实践]Kubeadm 1.9 HA 高可用 集群 本地离线镜像部署

    k8s介绍 k8s 发展速度很快,目前很多大的公司容器集群都基于该项目,如京东,腾讯,滴滴,瓜子二手车,北森等等. kubernetes1.9版本发布2017年12月15日,每是那三个月一个迭代, W ...

  9. Spring Security 解析(三) —— 个性化认证 以及 RememberMe 实现

    Spring Security 解析(三) -- 个性化认证 以及 RememberMe 实现   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把 ...

  10. Python进阶----pymysql的安装与使用,mysql数据库的备份和恢复,mysql的事务和锁

    Python进阶----pymysql的安装与使用,mysql数据库的备份和恢复,mysql的事务和锁 一丶安装 pip install PyMySQL 二丶pymysql连接数据库 ### 语法: ...