ZOJ3261 Connections in Galaxy War 并查集
分析:对于这种删边操作,我们通常可以先读进来,然后转化离线进行倒着加边
#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 并查集的更多相关文章
- ZOJ-3261 Connections in Galaxy War 并查集 离线操作
题目链接:https://cn.vjudge.net/problem/ZOJ-3261 题意 有n个星星,之间有m条边 现一边询问与x星连通的最大星的编号,一边拆开一些边 思路 一开始是真不会,甚至想 ...
- ZOJ3261:Connections in Galaxy War(逆向并查集)
Connections in Galaxy War Time Limit: 3 Seconds Memory Limit: 32768 KB 题目链接:http://acm.zju.edu. ...
- ZOJ3261 Connections in Galaxy War —— 反向并查集
题目链接:https://vjudge.net/problem/ZOJ-3261 In order to strengthen the defense ability, many stars in g ...
- 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 ...
- 洛谷 P1197 BZOJ 1015 [JSOI2008]星球大战 (ZOJ 3261 Connections in Galaxy War)
这两道题长得差不多,都有分裂集合的操作,都是先将所有操作离线,然后从最后一步开始倒着模拟,这样一来,分裂就变成合并,也就是从打击以后最终的零散状态,一步步合并,回到最开始所有星球都被连为一个整体的状态 ...
- 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(离线并查集)
Description In order to strengthen the defense ability, many stars in galaxy allied together and bui ...
- ZOJ-3261 Connections in Galaxy War---离线操作+逆序并查集
题目链接: https://cn.vjudge.net/problem/ZOJ-3261 题目大意: 给你一些点,还有一些边,每个点上都有一个权值,然后有一些询问,分为两种,query a 询问与a直 ...
随机推荐
- 一个基本jquery的评论留言模块
<div class="productDiscuss"> <div class="title"><span class=" ...
- asp.net 获取当前项目路径
方法一://获取当前项目的路径System.AppDomain.CurrentDomain.BaseDirectory.ToString(); // 得到的是当前项目的根目录取的值:F://Pro ...
- CSS弹性盒模型 box-flex
目前没有浏览器支持boc-flex属性. Firefox支持代替的-moz-box-flex属性 Safari.Opera以及Chrome支持替代的-webkit-box-flex属性 box-fle ...
- JS判断输入框值是否为空
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Python 强大而易用的文件操作(转载)
在Python中可以很方便地做一些诸如浏览目录,创建文件夹,删除文件夹等等的操作. 对文件系统的访问大多通过os模块来实现,因为Python是多平台的,os模块只是前端,具体的实现还是由具体的系统来完 ...
- C语言小结之结构类型
C语言小结之结构类型 @刁钻的游戏 (1)枚举型类型enum COLOR {BLACK,RED,BLUE};//声明一种新的数据类型,其值分别为0,1,2但是用BLACK/RED/BLUE代表也可以这 ...
- 【viewResolver】 springmvc jsp
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> < ...
- Python之添加新元素
现在,班里有3名同学: >>> L = ['Adam', 'Lisa', 'Bart'] 今天,班里转来一名新同学 Paul,如何把新同学添加到现有的 list 中呢? 第一个办法是 ...
- 服务器环境搭建系列(一)-Apache篇
一.Apache 1.解压缩tar包httpd-2.2.22.tar.gz,这里默认放在/opt下 tar -zxvf httpd-2.2.22.tar.gz 2.进入解压缩后的文件夹 cd http ...
- jQuery滑动导航菜单
js: $(function(){ $("ul.sub").parent().append("<span></span>"); $(&q ...