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直 ...
随机推荐
- ECshop网店系统百万级商品量性能优化-加快首页访问速度
如果ECshop的商品数达到几万,十几万的时候,如果首页没有缓存,第一次访问的时候,你会发现其慢无比,原因就是清空了Cache后或者没有Cache的情况下,ECshop会Bulid一些Cache数据, ...
- jenkins 重新设置 管理员密码
由于服务器瘫痪,修好之后经常不上,就把jenkins的管理密码忘掉了. 查阅了网上所有方案之后发现没有一个 能正确修改密码的,特此列出下列网上的方法 第一.设成无需密码验证的(网上有教程,不过并不能修 ...
- RHEL 6.4 64bit kettle5.01导入xlsx格式的excel时报错
环境:RHEL 6.4 64bit : kettle5.01:xlsx格式的excel 创建的job,在spoon里面运行都没有问题(Linux和windows) 在windows的命令行运行也没有问 ...
- 机器学习实战:数据预处理之独热编码(One-Hot Encoding)
问题由来 在很多机器学习任务中,特征并不总是连续值,而有可能是分类值. 例如,考虑一下的三个特征: ["male", "female"] ["from ...
- MVC5 学习笔记2
去除VS Browser Link废代码 在webconfig中添加 <configuration> <appSettings> <add key="vs:En ...
- Nhibernate配置和访问数据问题
今天开始用Nhibernate做为自己的ORM,但是做的过程中确实遇到了好多问题,现在将问题收集起来以防日后出现相同的问题, 总结下: 这就是我的整个项目,现在配置下hibernate.cfg.xml ...
- php分页类的二种调用方法(转载)
php分页类的二种调用方法 原文地址:http://www.xfcodes.com/php/fenye/25584.htm 导读:php分页类的二种调用用法,ajax调用php分页类,非ajax方式调 ...
- BZOJ 3969 Low Power 解题报告
我们首先将所有电池排序,那么我们可以找到一组最优方案,使得一台机器的能量之差是相邻两电池的能量之差. 然后我们就二分这个答案,从前往后贪心地选这个数对,然后看是否所有的数对都是满足条件的. 假设这个数 ...
- java对xml文件做增删改查
http://www.cnblogs.com/wangchenyang/archive/2011/08/23/2150530.html http://www.blogjava.net/weishuan ...
- [Gauss]POJ2065 SETI
题意: *代表0,a-z代表1-26 题目第三行给了一个公式 f (k) = $\sum\limits_{i=0}^{n-1} a_i k^i \pmod{P}$ {f(i)是输入的一串字符串中第i ...