#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 离线操作+逆序并查集 并查集删边的更多相关文章

  1. (并查集)Connections in Galaxy War -- zoj --3261 还没写

    链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 http://acm.hust.edu.cn/vjudge/ ...

  2. Connections in Galaxy War ZOJ - 3261 (并查集)

    点权并查集的反向离线操作 题目大意:有n个stars,每一个都一定的“颜值”.然后stars与stars之间可以相连,query c表示再与c相连的stars中,颜值比c高的,stars的标号,如果有 ...

  3. L - Connections in Galaxy War - zoj 3261

    题意:有一个帝国在打仗,敌方会搞一些破坏,总共用N个阵地,每个阵地都有一个武力值,当第一地方收到攻击的时候他可以进行求助,当然求助的对象只能是武力值比他高的,如果求助失败就输出 ‘-1’, 求助成功就 ...

  4. 洛谷 P1197 BZOJ 1015 [JSOI2008]星球大战 (ZOJ 3261 Connections in Galaxy War)

    这两道题长得差不多,都有分裂集合的操作,都是先将所有操作离线,然后从最后一步开始倒着模拟,这样一来,分裂就变成合并,也就是从打击以后最终的零散状态,一步步合并,回到最开始所有星球都被连为一个整体的状态 ...

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

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

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

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

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

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

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

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

  9. zoj 3261 Connections in Galaxy War

    点击打开链接zoj 3261 思路: 带权并查集 分析: 1 题目说的是有n个星球0~n-1,每个星球都有一个战斗值.n个星球之间有一些联系,并且n个星球之间会有互相伤害 2 根本没有思路的题,看了网 ...

随机推荐

  1. JumpServer部署与管理

    一.JumpServer 堡垒机概述 JumpServer由Python/Django进行开发.使用GNU GPL v2.0开源协议.也是全球首款完全开源的堡垒机.同时配备了业界领先的Web Term ...

  2. 使用github--stanfordnlp--glove训练自己的数据词向量

    1.准备语料 准备好自己的语料,保存为txt,每行一个句子或一段话,注意要分好词.将分好词的语料保存为×××.txt 2.准备源码 下载地址:https://github.com/stanfordnl ...

  3. mysql 启动,停止,重启

    启动mysql: 方式一:sudo /etc/init.d/mysql start  方式二:sudo start mysql 方式三:sudo service mysql start sudo ./ ...

  4. 【转】关于apt源配置的问题

    涉及的基本配置文件: apt核心配置文件集中在 /etc/apt 其中,管理软件来源的配置文件如下 sources.list                           // 主要软件源 so ...

  5. css沉默

    css变色龙实现. ==== css 1 水平居中和垂直居中. 2 css布局方式. 3 你写过UI框架么.

  6. thinkphp v5.1.36 LTS 如果设置跨域访问

    修改route/route.php中的路由例如 Route::get('new/:id', 'News/read') ->ext('html') ->header('Access-Cont ...

  7. Jenkins 插件使用国内镜像源-解决插件下载慢的问题

    问题 我们在Jenkins里面经常会遇到安装插件很慢,这是由于我们使用的是更新中心镜像默认为国外的源.现在我们可以进行设置为国内镜像源,来解决安装插件慢的问题. 解决办法 安装插件localizati ...

  8. 1Python学习CentOS 7 Linux环境搭建

    鉴于python3目前已成流行之势,而各发行版Linux依然是自带python2.x,笔者尝试在centos7下,部署Python3.x与2.x共存环境 本文参考博主良哥95网址https://blo ...

  9. cookie的设置与取值

    设置cookie function cookie(key, value, options) { let days let time let result // A key and value were ...

  10. SAP 序列号与库存关联起来?

    SAP 序列号与库存关联起来? SAP系统标准功能可以实现序列号管理.其系统配置也不复杂,但是不少企业却使用不起来.笔者参与的诸多项目里,只有现在所在的项目里有启用序列号管理.基于项目客户所在行业,以 ...