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 题意 银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可 ...
随机推荐
- IIS 7安装ModSecurity实现WAF功能
ModSecurity 是一款开源Web应用防火墙,支持Apache/Nginx/IIS,可作为服务器基础安全设施,还是不错的选择. 系统环境:window 2008 R2+IIS 7 0X01 Mo ...
- Splash wait() 方法
wait()方法用于控制页面的等待时间,如下,实现访问淘宝并等待2秒,随后返回淘宝页面的源代码: function main(splash) splash:go("https://www.t ...
- Unity使用OpenGL绘制线段
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ShowGrid ...
- 在 Ubuntu 13.10 安装 PyCharm 3.0.1 & Oracle JDK
由于授权问题,在较新的Linux发行版本中都不再包含Oracle Java,取而代之的是OpenJDK.Ubuntu也是如此. OpenJDK能满足大部分的应用程序运行条件,但PyCharm无法在Op ...
- pojo与DTO的区别
ational Mapping(对象关系映射)的缩写.通俗点讲,就是将对象与关系数据库绑定,用对象来表示关系数据.在O/R Mapping的世界里,有两个基本的也是重要的东东需要了解,即VO,PO. ...
- springmvc接收前台(可以是ajax)传来的数组list,map,set等集合,复杂对象集合等图文详解
参考帖子: http://blog.csdn.net/wabiaozia/article/details/50803581 方法参考: { "token":"" ...
- Matlab练习——素数查找
输入数字,0结束,判断输入的数字中的素数 clc; %清空命令行窗口的数据 clear; %清除工作空间的变量 k = ; n = ; %素数的个数 zzs(k) = input('请输入正整数: ' ...
- 【Linux基础】Linux基础命令行学习笔记
绝对路径:cd /home/python相对路径:cd Downloads . 表示:当前那路径..表示:当前路径的上一层../.. 表示:当前路径的上二层 没有...或者以上的 ls: ls 查看当 ...
- Delphi应用程序的调试(一)
集成式调试器是Delphi IDE的一个重要特性.该调试器使用户能方便地设置断点.监视变量.检查对象等等.在运行程序时,使用该调试器能快速查找出程序发生了什么(或未发生什么).一个号的调试器对程序开发 ...
- 【Eclipse】一个简单的 RCP 应用 —— 显示Eclipse 的启动时间。
1 创建一个插件项目 1.1 File - New - Plug-in Project 注: 1 如果 New 下没有 Plug-in Project , 到 Other 里面去找. 2 如上截图的下 ...