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 根本没有思路的题,看了网 ...
随机推荐
- 【python爬虫】windoes的爬虫中文乱码现象,通用转码解决
page = session.get(url="https://www.qidian.com/") page.encoding = page.apparent_encoding p ...
- 【转载】SPI总线和I2C总线的异同点
来源:https://blog.csdn.net/lishun1422840684/article/details/77776763 总结的简单.明了.适用! 一:SPI接口的全称是"Ser ...
- HTTP下帐号密码的截取
用到工具: arpspoof -->>IP欺骗 ettercap -->>抓包 攻击者:192.168.100.110 kali 实验者:192.168.100 ...
- Spring Boot自动配置如何工作
通过使用Mongo和MySQL DB实现的示例,深入了解Spring Boot的@Conditional注释世界. 在我以前的文章“为什么选择Spring Boot?”中,我们讨论了如何创建Sprin ...
- 在Idea中jdk11和jdk8环境变量的切换
先配置好jdk11和jdk8环境变量 idea(2019.3)中jdk版本切换(jdk8和jdk11) 快捷键ctrl + shift + alt +s ,将jdk11修改为jdk1.8 完成后,显示 ...
- Win10多用户同时登陆远程桌面
想记录一下最近解决的一些问题,发现还是博客最合适,虽然之前从来没写过,希望以后能养成这个好习惯. 家里有一台台式机装着Win10,还有一台macbook,平时遇到需要用Win系统又不想坐在书桌前时,我 ...
- SWIG 3 中文手册——10. 参数处理
目录 10 参数处理 10.1 typemaps.i 库 10.1.1 引言 10.1.2 输入参数 10.1.3 输出参数 10.1.4 输入 / 输出参数 10.1.5 使用不同的名称 10.2 ...
- linux中 nodejs 安装 sqlite3 出现的问题
错误代码类似:Error: Cannot find module '/root/QuickNote/node_modules/sqlite3/lib/binding/node-v57-linux-x6 ...
- 安装 mysqlclient 报 mysql_config not found
安装 mysqlclient 报 mysql_config not found raise EnvironmentError("%s not found" % (mysql_con ...
- 面试想拿 10K,HR 说你只值 7K,该怎样回答或者反驳?
当HR压你价,说你只值7K时,你可以流畅地回答,记住,是流畅,不能犹豫. 礼貌地说:"7K是吗?了解了.嗯~其实我对贵司的面试官印象很好.只不过,现在我的手头上已经有一份11K的offer. ...