Connections in Galaxy War----zoj3261
题目链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261
题意:有n个星球编号为0—n-1;能量分别为p[i];有m句话,每句话输入a,b表示星球a和星球b可以相通的;
但是由于银河之战,破坏了一些通道
接下来有Q句话:destroy a b代表ab之间的通道被破坏;
query a代表求a可以向哪个星球求助,并输出编号,如果没有就输出-1;
各个星球只像能量值比自己大的星球求助,而且是尽量找到最大能量值的星球求助。
如果有多个能量值一样的星球可以求助,则找编号小的。
思路:
输入时记录每一条边
记录每一个操作和销毁的边。
输入结束后先用并查集加入所有没有被销毁的边
然后再逆序操作记录结果,此时遇到 destroy 则加入销毁的边 ,遇到 query 直接查找即可。
最终逆序输出结果。
不同的测试数据有空行。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<map>
#define N 50010
#define INF 0xfffffff
using namespace std; struct node
{
int op,x,y;/// 1代表查询,2代表破坏;
} a[N],b[N]; int f[N], p[N], n, m, ans[N]; map<int,int> maps[N]; int Find(int x)
{
if(x != f[x])
f[x] = Find(f[x]);
return f[x];
} void Union(int x, int y)
{
int px = Find(x);
int py = Find(y);
///合并的时候要向能量较大的那边合并,如果能量相等向编号小的那边合并;
if(p[px] > p[py])
f[py] = px;
else if(p[px] < p[py])
f[px] = py;
else
{
if(px <= py)
f[py] = px;
else
f[px] = py;
}
}
void Init()
{
int i;
memset(a, , sizeof(a));
memset(b, , sizeof(b));
for(i=; i<= n;i++)
{
maps[i].clear();
f[i] = i;
p[i] = ;
}
}
int main()
{
char str[];
int i, j, y, x, Q, flag=;
while(scanf("%d", &n)!=EOF)
{
Init(); for(i=; i<n; i++)
scanf("%d", &p[i]); scanf("%d", &m);
for(i=; i<m; i++)
{
scanf("%d%d", &x, &y);
if(x > y) swap(x, y); b[i].x = x;b[i].y = y; maps[x][y] = ;//代表x和y之间的通道存在;
} scanf("%d", &Q);
for(i=; i<Q; i++)
{
scanf("%s", str);
if(str[] == 'q')
{
scanf("%d", &x);
a[i].op = ;a[i].x = x;
}
else
{
scanf("%d %d", &x, &y);
if(x > y) swap(x, y); maps[x][y] = ;///x和y之间的路没路; a[i].op = ;a[i].x = x;a[i].y = y;
}
}
for(i=; i<m; i++)///把没有被破坏的边放到一起;
{
if(maps[b[i].x][b[i].y]==)
Union(b[i].x, b[i].y);
}
j=;
for(i=Q-; i>=; i--)///逆序处理每个操作;
{
if(a[i].op == )
{
int px = Find(a[i].x);
if(p[px] > p[a[i].x])///只能向比自己能量大的求助;
ans[j++] = px;
else
ans[j++] = -;
}
else if(a[i].op == )
Union(a[i].x, a[i].y);
} if(flag)printf("\n");
flag = ; for(i=j-; i>=; i--)
printf("%d\n", ans[i]); }
return ;
}
Connections in Galaxy War----zoj3261的更多相关文章
- ZOJ3261:Connections in Galaxy War(逆向并查集)
Connections in Galaxy War Time Limit: 3 Seconds Memory Limit: 32768 KB 题目链接:http://acm.zju.edu. ...
- 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)
这两道题长得差不多,都有分裂集合的操作,都是先将所有操作离线,然后从最后一步开始倒着模拟,这样一来,分裂就变成合并,也就是从打击以后最终的零散状态,一步步合并,回到最开始所有星球都被连为一个整体的状态 ...
- ZOJ3261 Connections in Galaxy War —— 反向并查集
题目链接:https://vjudge.net/problem/ZOJ-3261 In order to strengthen the defense ability, many stars in g ...
- 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 题意 有n个星星,之间有m条边 现一边询问与x星连通的最大星的编号,一边拆开一些边 思路 一开始是真不会,甚至想 ...
- ZOJ3261 Connections in Galaxy War 并查集
分析:对于这种删边操作,我们通常可以先读进来,然后转化离线进行倒着加边 #include <stdio.h> #include <string.h> #include < ...
- ZOJ - 3261 Connections in Galaxy War(并查集删边)
https://cn.vjudge.net/problem/ZOJ-3261 题意 银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可 ...
随机推荐
- ISO27001信息安全管理体系
0x00 前言 初入甲方,刚开始接触的应该就是ISO27001信息安全管理体系,你拿到的应该就是一整套安全管理类的文档.在甲方,稍微有点规模的公司很注重制度和流程,岗位职责分工明细,那么这些安全管理制 ...
- PHP代码审计笔记--文件包含漏洞
有限制的本地文件包含: <?php include($_GET['file'].".php"); ?> %00截断: ?file=C://Windows//win.in ...
- Streaming 101
开宗明义!本文根据Google Beam大神Tyler Akidau的系列文章<The world beyond batch: Streaming 101>(批处理之外的流式世界)整理而成 ...
- 【delphi】delphi的TAdoQuery读取Excel数据
1. 连接 需要设置TAdoQuery的连接串Connection,将其指向excel文件: 'Provider=Microsoft.ACE.OLEDB.12.0;Data Source=' + ex ...
- 【laravel5.6】 laravel 接口 接管 自定义异常类
1 app\exceptions 目录下 新建 Apiexception.php <?php namespace App\Exceptions; /*** * API 自定义异常类 */ us ...
- jQuery弹出层插件大全
1.thickbox 目前用的比较多的,最新版本是thickbox3.1 下载地址:http://jquery.com/demo/thickbox/#examples 2.colorBox 官方网站: ...
- 原生js--类、原型、构造函数
1.类和原型:原型对象是类的核心,类的所有实例都从同一个原型上继承属性和方法,原型对象是类的唯一标识 2.类和构造函数:构造函数通过new关键字来创建对象,构造函数的prototype属性被用作新对象 ...
- Python OS 文件/目录方法
Python OS 文件/目录方法 os 模块提供了非常丰富的方法用来处理文件和目录.常用的方法如下表所示: 序号 方法及描述 1 os.access(path, mode) 检验权限模式 2 os. ...
- SharpGL学习笔记(四) 正射投影
上节谈到投影变换分为透视投影(perspective projection)和正射投影(orthographic projection)两种. 透视投影我们已经介绍过了, 现在谈谈正视投影. 正射投影 ...
- Android Studio 3.0.1 版本包下载
Android Studio 3.0.1 发布了,这是对 Android Studio 3.0 的一个小的更新,包括一般错误修复和性能改进 下载地址: Windows 64 位:https://dl. ...