Connections in Galaxy War ZOJ - 3261 离线操作+逆序并查集 并查集删边
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<map>
#include<vector>
#define cle(a) memset(a,0,sizeof(a))
using namespace std;
const int N=+;
int w[];
bool cmp(int a,int b){
return a>b;
}
int n,q,m,fa[N],arr[][],ans[];
vector<pair<int,int> >vp;
map<pair<int,int>,int>mp;
void init(){
for(int i=;i<=n+;i++)fa[i]=i;
mp.clear();
vp.clear();
cle(ans);
cle(arr);
}
int find(int x){
if(fa[x]!=x)
fa[x]=find(fa[x]);
return fa[x];
}
void Union(int a,int b)
{
int x=find(a);
int y=find(b);
if(x==y)
return;
if(w[x]>w[y])
fa[y]=x;
else if(w[x]<w[y])
fa[x]=y;
else{ //这里 的判断要注意
if(x<y)
fa[y]=x;
else
fa[x]=y;
}
}
int main()
{
int mark=;
while(cin>>n)
{
if(mark)
printf("\n");
init();
//每个点的价值
for(int i=;i<n;i++)
scanf("%d",&w[i]);
scanf("%d",&m);
//边
for(int i=;i<=m;i++)
{
scanf("%d%d",&arr[i][],&arr[i][]);
//大的在前面,为后面建边做准备
if(arr[i][]>arr[i][])
swap(arr[i][],arr[i][]);
}
scanf("%d",&q);
char s[];
int t,p;
for(int i=;i<=q;i++)
{
scanf("%s",s);
if(s[]=='q')
{
scanf("%d",&t);
vp.push_back({t,-});
}
else{
scanf("%d%d",&t,&p);
if(t>p)
swap(t,p);
//标记已经建过边了,相当于标记要被拆掉
mp[{t,p}]=;
vp.push_back({t,p});
}
}
//先把不会被拆掉的边建上
for(int i=;i<=m;i++)
if(!mp[{arr[i][],arr[i][]}])
Union(arr[i][],arr[i][]);
//然后从后往前遍历操作
//如果是查询,直接做
//如果是拆边,就建上
int j=;
for(int i=vp.size()-;i>=;i--){
int a=vp[i].first;
int b=vp[i].second;
//如果是查询
if(b==-)
{
//找到父节点
int c=find(a);
//如果相同,就是-1
if(w[a]>=w[c])
c=-;
//保存答案
ans[j++]=c;
}
else
Union(a,b);
}
for(int i=j-;i>=;i--)
printf("%d\n",ans[i]);
mark=;
}
return ;
}
Connections in Galaxy War ZOJ - 3261 离线操作+逆序并查集 并查集删边的更多相关文章
- (并查集)Connections in Galaxy War -- zoj --3261 还没写
链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 http://acm.hust.edu.cn/vjudge/ ...
- Connections in Galaxy War ZOJ - 3261 (并查集)
点权并查集的反向离线操作 题目大意:有n个stars,每一个都一定的“颜值”.然后stars与stars之间可以相连,query c表示再与c相连的stars中,颜值比c高的,stars的标号,如果有 ...
- L - Connections in Galaxy War - zoj 3261
题意:有一个帝国在打仗,敌方会搞一些破坏,总共用N个阵地,每个阵地都有一个武力值,当第一地方收到攻击的时候他可以进行求助,当然求助的对象只能是武力值比他高的,如果求助失败就输出 ‘-1’, 求助成功就 ...
- 洛谷 P1197 BZOJ 1015 [JSOI2008]星球大战 (ZOJ 3261 Connections in Galaxy War)
这两道题长得差不多,都有分裂集合的操作,都是先将所有操作离线,然后从最后一步开始倒着模拟,这样一来,分裂就变成合并,也就是从打击以后最终的零散状态,一步步合并,回到最开始所有星球都被连为一个整体的状态 ...
- ZOJ3261:Connections in Galaxy War(逆向并查集)
Connections in Galaxy War Time Limit: 3 Seconds Memory Limit: 32768 KB 题目链接:http://acm.zju.edu. ...
- Connections in Galaxy War (逆向并查集)题解
Connections in Galaxy War In order to strengthen the defense ability, many stars in galaxy allied to ...
- Connections in Galaxy War(逆向并查集)
Connections in Galaxy War http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563 Time Limit ...
- ZOJ 3261 - Connections in Galaxy War ,并查集删边
In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ...
- zoj 3261 Connections in Galaxy War
点击打开链接zoj 3261 思路: 带权并查集 分析: 1 题目说的是有n个星球0~n-1,每个星球都有一个战斗值.n个星球之间有一些联系,并且n个星球之间会有互相伤害 2 根本没有思路的题,看了网 ...
随机推荐
- java架构之路-(微服务专题)nacos集群精讲实战
上次回顾: 上次博客,我们主要说了微服务的发展历程和nacos集群单机的搭建,单机需要-m standalone启动,集群建议使用nginx做一下反向代理,自行保证mysql和ngxin的高可用. 本 ...
- awk sed 命令
awk awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大. 简单来说awk就是把文件逐行的读入,以 空格或TAB 为默认分隔符 将每行 ...
- Linux 配置ip 子接口 多网卡绑定
linux系统配置ip地址,图形化界面略过,这里只介绍文本行.做以下设置注意是否有此权限 查看当前路由及网关信息: [root@localhost ~]# netstat -r Kernel IP r ...
- [Windows]远程管理服务WinRM远程管理Windows服务器 Invalid use of command line. Type "winrm -?" for help.
运行环境 Windows 2012 R2 1. Windows需要打开WinRM服务,Server系统默认打开,默认端口5985 # WinRM服务查看 PS C:\Users\Administrat ...
- PHP关于mb_substr不能起作用的问题
mb_substr不能起作用最大的原因是因为没有在php.ini文件没有把 ;extension=mbstring 前面的 :号去掉
- centos7上安装docker社区版
container(容器) docker(集装箱) 容器的优点 1. 启动速度快 2. 节省资源 3. 兼容性高 保证机器正常上网 #ping www.baidu.com CPU需要支持虚拟化 # g ...
- hexo--定制开发
新建页面 hexo new page "新建博文章的名称" 这时会在工程的source目录下新建about目录,里面新建index.md 在主题的_configy.yml中配置新页 ...
- typeof和类型转换
编程形式 ① 面向过程 ② 面向对象 ③ Js既面向过程又面向对象 typeof(数据) 1)typeof(数据)返回该数据是什么类型的 2)写法: ① typeof(数据) ② typeof 数据 ...
- win10上使用linux命令
(1)可以用windows自带的powershell,但是 ll,vim等命令不能使用 (2)Windows更新==>针对开发人员==>开启开发人员模式,然后在控制面板==>程序与功 ...
- linux中vim常用操作
三种模式 # 命令模式 vim 文件名 # 插入模式 按a/i/o 进行插入模式 按esc 重新进入命令模式 # 编辑模式 按:(冒号)进入编辑模式 插入命令 命令 作用 a 在光标所在字符后插入 A ...