ZOJ-3261 Connections in Galaxy War---离线操作+逆序并查集
题目链接:
https://cn.vjudge.net/problem/ZOJ-3261
题目大意:
给你一些点,还有一些边,每个点上都有一个权值,然后有一些询问,分为两种,
query a 询问与a直接或者间接想连的点中最大权值的是那个点,输出那个点,如果那个点的权值小于等于a的权值,那么就输出-1,还有另一种操作就是destroy a b意思是删除a b的关系。
解题思路:
此处需要删边,应该想到逆序离线处理。先将所有需要删除的边直接删除,将所有操作存下来逆序处理,对于需要删的边就变成添加这条边。求与a相连的权值最大的点,就直接求a这个连通块中的最大权值的点,可以用并查集求,添加边的时候就合并两个连通块即可。
注意合并连通块时,权值大的为父亲,如果权值相同,那么编号小的为父亲
在最后判断是否存在为-1的时候,需要用连通块根节点的权值和当前询问节点权值进行比较,不可用下标相等比较
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + ;
int n, m;
int a[maxn];//每个点的权值
vector<int>Map[maxn];//存边(只保留编号小的指向编号大的边)
struct node//存储操作
{
int type, x, y;
}op[maxn];
vector<int>ans;
int fa[maxn];
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(a[x] < a[y])
{
fa[x] = y;
}
else if(a[x] > a[y])
{
fa[y] = x;
}
else if(x != y)
{
if(x < y)
{
fa[y] = x;
}
else
{
fa[x] = y;
}
}
}
int main()
{
int flag = ;
while(scanf("%d", &n) != EOF)
{
if(flag++)printf("\n");
memset(op, , sizeof(op));
memset(a, , sizeof(a));
ans.clear();
for(int i = ; i < n; i++)scanf("%d", &a[i]), Map[i].clear(), fa[i] = i; scanf("%d", &m);
int u, v;
while(m--)
{
scanf("%d%d", &u, &v);
if(u > v)swap(u, v);
Map[u].push_back(v);
}
scanf("%d", &m);
char s[];
for(int i = ; i <= m; i++)
{
scanf("%s", s);
if(s[] == 'q')
op[i].type = , scanf("%d", &op[i].x);
else //先将边全部删除,逆序操作,逐渐增加边
{
op[i].type = ;
scanf("%d%d", &u, &v);
if(u > v)swap(u, v);
op[i].x = u, op[i].y = v;
Map[u].erase(find(Map[u].begin(), Map[u].end(), v));
}
}
for(int i = ; i < n; i++)
{
for(int j = ; j < Map[i].size(); j++)
{
Union(i, Map[i][j]);
}
}
for(int i = m; i >= ; i--)
{
if(op[i].type == )
{
u = Find(op[i].x);
if(a[u] <= a[op[i].x])u = -;
//不能仅仅判断u != op[i].x 因为根节点可能为其他点并且权值等于该节点权值,此时该节点也应该认为无连接
ans.push_back(u);
}
else
{
Union(op[i].x, op[i].y);
}
}
for(int i = ans.size() - ; i >= ; i--)
printf("%d\n", ans[i]);
}
return ;
}
ZOJ-3261 Connections in Galaxy War---离线操作+逆序并查集的更多相关文章
- ZOJ 3261 Connections in Galaxy War (逆向+带权并查集)
题意:有N个星球,每个星球有自己的武力值.星球之间有M条无向边,连通的两个点可以相互呼叫支援,前提是对方的武力值要大于自己.当武力值最大的伙伴有多个时,选择编号最小的.有Q次操作,destroy为切断 ...
- 洛谷 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
点击打开链接zoj 3261 思路: 带权并查集 分析: 1 题目说的是有n个星球0~n-1,每个星球都有一个战斗值.n个星球之间有一些联系,并且n个星球之间会有互相伤害 2 根本没有思路的题,看了网 ...
- 题解报告: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(并查集逆向加边)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 题意:有很多颗星球,各自有武力值,星球间有一些联系通道,现 ...
- Connections in Galaxy War ZOJ - 3261 离线操作+逆序并查集 并查集删边
#include<iostream> #include<cstring> #include<stdio.h> #include<map> #includ ...
- ZOJ 3261 Connections in Galaxy War(逆向并查集)
参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...
- ZOJ - 3261 Connections in Galaxy War(并查集删边)
https://cn.vjudge.net/problem/ZOJ-3261 题意 银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可 ...
随机推荐
- 常用 JavaScript 小技巧及原理详解
善于利用JS中的小知识的利用,可以很简洁的编写代码 1. 使用!!模拟Boolean()函数 原理:逻辑非操作一个数据对象时,会先将数据对象转换为布尔值,然后取反,两个!!重复取反,就实现了转换为布尔 ...
- 在用户控件(ASCX)创建用户控件(ASCX)
"我建了两个ascx,ascxA,ascxBascxA中放了一个PlaceHold,ascxB中放了一个textBoxascxA在page_load中动态创建了5个ascxB但是页面上什么都 ...
- 怎么用PHP发送HTTP请求(POST请求、GET请求)?
file_get_contents版本: 01 /** 02 * 发送post请求 03 * @param string $url 请求地址 04 * @param array $post_data ...
- node错误集合
1.端口被占用 node .\app.js events.js:167 throw er; // Unhandled 'error' even 解决办法:8888端口被占用了,更改一个端口就好 2. ...
- [PHP] PHP的纯CPU基准测试(PHP5.5.9 vs PHP7.2.1)
PHP的纯CPU基准测试(PHP5.5.9 vs PHP7.2.1): 1.bench.php 可在PHP源代码的 php-src/Zend 目录 2.micro_bench.php 也可以在 PHP ...
- [javaSE] GUI(事件监听机制)
外部动作——>事件源(组件)——>事件对象——>监听器 获取Frame对象,与上节一样 调用Frame对象的addWindowListener()方法,参数:WindowListen ...
- JDBC入门(1)—— 入门案例
JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组 ...
- drupal7 为视图添加 过滤标准 内容类型
1.单击 FILTER CRITERIA 右边的“添加”按钮 2.在弹出的对话框中输入“类型”,单击搜索结果中的“内容:类型” 3.确定之后,选择需要的内容类型即可,例如添加“书评”内容类型的过滤 4 ...
- Google APAC----Africa 2010, Qualification Round(Problem C. T9 Spelling)----Perl 解法
原题地址链接:https://code.google.com/codejam/contest/351101/dashboard#s=p2 问题描述: Problem The Latin alphabe ...
- Latex表格太宽处理方法 (How to shorten Latex table length)
当表格太宽时, 为了能在页面中显示完整, 可以缩小表格, 或者横排.缩小表格的好处是, 不用倒转页面阅读, 坏处是原始宽度不同的表格, 被缩小后, 字体不一, 不美观. 虽然可以调整参数使得所有表格字 ...