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 根本没有思路的题,看了网 ...
随机推荐
- 内网ICMP隧道构建之icmpsh
下载地址: https://github.com/inquisb/icmpsh#usage kali下载 git clone https://github.com/inquisb/icmpsh.git ...
- Coroutine 预激装饰器
预激装饰器 讨论如何终止协程之前,我们要先谈谈如何启动协程.使用协程之前必须预激,可是这一 步容易忘记.为了避免忘记,可以在协程上使用一个特殊的装饰器.接下来介绍这样一个 装饰器. 预激协程的装饰器, ...
- CNN中的feature map
个人学习CNN的一些笔记,比较基础,整合了其他博客的内容 feature map的理解在cnn的每个卷积层,数据都是以三维形式存在的.你可以把它看成许多个二维图片叠在一起(像豆腐皮竖直的贴成豆腐块一样 ...
- php 绘制验证码 示例
<?php header("content-type:image/jpeg"); session_start();//开启session //宽高 字体大小 $width=1 ...
- FastDFS 配置文件 storage.conf
FastDFS 版本5.05 配置文件分为三部分 控制器:tracker.conf存储器:storage.conf 客户端:client.conf 文件位置:/etc/fdfsstorage.co ...
- 高级安全Windows防火墙概述以及最佳实践
本文简单介绍Windows防火墙的概念,给出使用场景并列出了常见的防火墙操作. 简介 在Windows NT6.0之后微软推出了高级安全Windows防火墙(简称WFAS),高级安全Windows防火 ...
- js循环小练习
function fn(){ //打印三角形 for(var i = 1 ; i <= 30 ; i++){ for(var x = 1; x <= i; x++ ){ document. ...
- 常用js封装
//获取url参数 function getUrlParams(name, url) { if (!url) url = location.href; name = name.replace(/[\[ ...
- Linux学习Day6:编写Shell脚本
Shell脚本命令的工作方式有两种: 交互式(Interactive):用户每输入一条命令就立即执行. 批处理(Batch):由用户事先编写好一个完整的Shell脚本,Shell会一次性执行脚本中诸多 ...
- 【干货】Python基础——列表
1.列表的创建 列表是多个.有序.可重复的元素集合,数据包装在“[]”里,列表中的元素可以是任何类型,甚至可以是一个列表.创建列表有两种方法: A 对于规模比较小的列表可以直接定义和赋值,列表可以嵌套 ...