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直 ...
随机推荐
- phpstudy 下开启openssl
1.在php.ini里面的extension=php_openssl.dll是开启的状态,没有被注释. 2.里面的allow_url_fopen = On表示开启了. 3.如果你是Windows 64 ...
- C/C++代码检视实例
相关文章链接如下: 微软过桥问题与测试人员素养 等价类分法 新解 测试用例设计中的NP难题 90%程序员写不出无BUG的二分查找程序? C/C++代码检视要点 4.1 代码检视 ...
- Convert.ToInt32()与int.Parse()的区别 (转载)
Convert.ToInt32()与int.Parse()的区别(1)这两个方法的最大不同是它们对null值的处理:Convert.ToInt32(null)会返回0而不会产生任何异常,但int.Pa ...
- Apache的多路处理模块MPM:Prefork Worker Event
如何确认当前apache使用哪种模式 通过/etc/init.d/httpd中的来确认系统apache的运行脚本路径 apachectl=/usr/sbin/apachectl httpd=${HTT ...
- Spring 官方下载地址(非Maven)
现在spring的官网停止了使用zip包下载,只能使用maven,非常的不方便,分享如下网址可以使用zip包下载,是不是方便多了!~ 下载列表如下: spring-framework-3.2.8.RE ...
- 自定义弹出div对话框
<style type="text/css"> html,body{height:100%;overflow:hidden;} body,div,h2{margin:0 ...
- Cognos 多维源目录树的任何单个维度中显示的最大项目数(默认为50)的设置规则
问题描述: 具体设置 设置成功.
- ARM的工作环境和工作模式
工作环境: 可以称之为“数据环境,”数据总线16位或者32位,应用于不同的情况下.16位的情况下,工作速度快,代码密度高. 工作模式: 设置程序数据所处的状态,为移植操作系统提供方便.
- JDBC和DBUtils区别(查询时jdbc只能返回ResultSet需要po转vo,dbutils返回的BeanListHandler与BeanHandler对应集合与对象)
17:34 2013/6/7 JDBC //添加客户 public void addNewCustomer(Customer c) throws DAOException { Connection c ...
- [转载]MongoDB学习 (六):查询
本文地址:http://www.cnblogs.com/egger/archive/2013/06/14/3135847.html 欢迎转载 ,请保留此链接๑•́ ₃•̀๑! 本文将介绍操作符的使用 ...