分析:对于这种删边操作,我们通常可以先读进来,然后转化离线进行倒着加边

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef pair<int,int>pii;
const int N=1e4+;
pii p[N<<];
int v[N],fa[N],n,m,q;
bool vis[N<<];
struct Ask{
int op,id,ans;
}ask[N*];
int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
void Union(int x,int y){
x=find(x),y=find(y);
if(x==y)return;
if(v[x]>v[y]||(v[x]==v[y]&&x<y))fa[y]=x;
else fa[x]=y;
}
int main()
{
bool flag=;
while(~scanf("%d",&n)){
if(flag)flag=;
else printf("\n");
for(int i=;i<n;++i){
scanf("%d",&v[i]),fa[i]=i;
}
memset(vis,,sizeof(vis));
scanf("%d",&m);
for(int i=;i<=m;++i){
scanf("%d%d",&p[i].first,&p[i].second);
if(p[i].first>p[i].second)swap(p[i].first,p[i].second);
}
sort(p+,p++m);
scanf("%d",&q);
for(int i=;i<=q;++i){
char s[];
int u,v;
scanf("%s%d",s,&u);
if(s[]=='q'){
ask[i].op=;
ask[i].id=u;
}
else{
ask[i].op=;
scanf("%d",&v);
if(u>v)swap(u,v);
ask[i].id=lower_bound(p+,p++m,make_pair(u,v))-p;
vis[ask[i].id]=;
}
}
for(int i=;i<=m;++i){
if(vis[i])continue;
Union(p[i].first,p[i].second);
}
for(int i=q;i>;--i){
if(ask[i].op==){
ask[i].ans=find(ask[i].id);
if(v[ask[i].ans]==v[ask[i].id])ask[i].ans=-;
}
else Union(p[ask[i].id].first,p[ask[i].id].second);
}
for(int i=;i<=q;++i)if(ask[i].op==)printf("%d\n",ask[i].ans);
}
return ;
}

ZOJ3261 Connections in Galaxy War 并查集的更多相关文章

  1. ZOJ-3261 Connections in Galaxy War 并查集 离线操作

    题目链接:https://cn.vjudge.net/problem/ZOJ-3261 题意 有n个星星,之间有m条边 现一边询问与x星连通的最大星的编号,一边拆开一些边 思路 一开始是真不会,甚至想 ...

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

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

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

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

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

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

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

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

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

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

  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(离线并查集)

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

  9. ZOJ-3261 Connections in Galaxy War---离线操作+逆序并查集

    题目链接: https://cn.vjudge.net/problem/ZOJ-3261 题目大意: 给你一些点,还有一些边,每个点上都有一个权值,然后有一些询问,分为两种,query a 询问与a直 ...

随机推荐

  1. 排序算法FIVE:插入排序InsertSort

    /** *插入排序思路:O(n^2) * 最外层一个循环,从第二个数到最后一个,变量为i * 每个数存储在key变量中 * 变量j,是左边已经排好序的数组的上限 * 判断key与前面每一个数比较 1, ...

  2. JQuery上传控件 jUploader 使用

    jUploader 1.0 Demo Download: jquery.jUploader-1.01.js 9.75kb Download: jquery.jUploader-1.01.min.js ...

  3. JS验证邮箱格式是否正确的代码

    验证邮箱格式是否正确的方法有很多,接下来为大家介绍下使用js是如何做到的 复制代码代码如下: /*  *验证邮箱格式是否正确  *参数strEmail,需要验证的邮箱  */ www.jbxue.co ...

  4. jquery控制左右箭头滚动图片列表

    jquery控制左右箭头滚动图片列表的实例. 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

  5. javascript操作html元素CSS属性

    下面先记录一下JS控制CSS所使用的方法. 1.使用javascript更改某个css class的属性... <style type="text/css"> .ori ...

  6. 阿里云服务器centos5.10安装lamp环境

    ==相关命令== 查看linux版本:cat /etc/redhat-release ==配置修改== 一.Apache配置 ------------------------------------- ...

  7. building hadoop2.4.1 on centos7[在centos7上面构建hadoop2.4.1]

    本文介绍在centos7上面通过hadoop2.4.1源码构建hadoop distribution 版本,即hadoop的运行版本. 为何要自己building,而不用Apache的distribu ...

  8. Lucas定理的理解与应用

    Lucas定理:用于计算组合数模除素数后的值,其实就是把(n,m)分别表示为p进制,累乘各位的可能取的个数,得到最终的结果: 推论:(n & m) == m则C(n,m)为奇数:即C(n,m) ...

  9. 中国.net域名网站的“前世今生”,那些年的光辉

    1987年9月的一天,中国的第一封电子邮件成功发出,邮件的内容大致是“跨越长城,走向世界”,在当时,没有人会想到十年后中国的互联网开始萌芽,并发展成今天的繁荣.1994年,“巴黎统筹委员会”的解散消除 ...

  10. C3p0的参数设置

    C3p0的参数设置:ComboPooledDataSource和BasicDataSource一样提供了一个用于关闭数据源的close()方法,这样我们就可以保证Spring容器关闭时数据源能够成功释 ...