题目链接:

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的更多相关文章

  1. ZOJ3261:Connections in Galaxy War(逆向并查集)

    Connections in Galaxy War Time Limit: 3 Seconds      Memory Limit: 32768 KB 题目链接:http://acm.zju.edu. ...

  2. Connections in Galaxy War (逆向并查集)题解

    Connections in Galaxy War In order to strengthen the defense ability, many stars in galaxy allied to ...

  3. Connections in Galaxy War(逆向并查集)

    Connections in Galaxy War http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563 Time Limit ...

  4. 洛谷 P1197 BZOJ 1015 [JSOI2008]星球大战 (ZOJ 3261 Connections in Galaxy War)

    这两道题长得差不多,都有分裂集合的操作,都是先将所有操作离线,然后从最后一步开始倒着模拟,这样一来,分裂就变成合并,也就是从打击以后最终的零散状态,一步步合并,回到最开始所有星球都被连为一个整体的状态 ...

  5. ZOJ3261 Connections in Galaxy War —— 反向并查集

    题目链接:https://vjudge.net/problem/ZOJ-3261 In order to strengthen the defense ability, many stars in g ...

  6. ZOJ 3261 - Connections in Galaxy War ,并查集删边

    In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ...

  7. 题解报告:zoj 3261 Connections in Galaxy War(离线并查集)

    Description In order to strengthen the defense ability, many stars in galaxy allied together and bui ...

  8. ZOJ-3261 Connections in Galaxy War 并查集 离线操作

    题目链接:https://cn.vjudge.net/problem/ZOJ-3261 题意 有n个星星,之间有m条边 现一边询问与x星连通的最大星的编号,一边拆开一些边 思路 一开始是真不会,甚至想 ...

  9. ZOJ3261 Connections in Galaxy War 并查集

    分析:对于这种删边操作,我们通常可以先读进来,然后转化离线进行倒着加边 #include <stdio.h> #include <string.h> #include < ...

  10. ZOJ - 3261 Connections in Galaxy War(并查集删边)

    https://cn.vjudge.net/problem/ZOJ-3261 题意 银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可 ...

随机推荐

  1. D盾 v2.0.6.42 测试记录

    0x01 前言 之前发了一篇博客<Bypass D盾_IIS防火墙SQL注入防御(多姿势)>,D哥第一时间联系我,对问题进行修复.这段时间与D哥聊了挺多关于D盾这款产品的话题,实在是很佩服 ...

  2. SaltStack 使用 Jinja2 模板

    Jinja2 是基于 python 的一个模板引擎,如下,使用 Jinja2 实现根据不同的操作系统分发不同的文件: [root@localhost ~]$ cat /srv/salt/test.sl ...

  3. 使用 urllib 分析 Robots 协议

    (1) Robots Exclusion Protocol,网络爬虫排除标准,也称爬虫协议.机器人协议,用来告诉爬虫和搜索引擎哪些页面可以抓取,哪些不可以抓取(2) Robots 协议以 robots ...

  4. error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version

    更新的软件可以解决 TortoiseGit-preview-2.5.7.0-20180127-b2d00f8-64bit.msi和Git-2.16.2-64-bit.exe. 链接地址为: https ...

  5. SQL - 只获取小时

    --时间小时加减 SELECT DATEADD(HOUR, -8, GETDATE()) FROM [Order] --使用convert转换时间格式获取小时,并转成int类型 SELECT CONV ...

  6. open-falcon之judge

    功能 judge 模块主要从transfer中接收数据,并从HBS中获取报警策略,然后进行阈值报警判断 从HBS获取报警策略 接收transfer 上报的数据,并存储最新几个点 判断阈值,产生报警事件 ...

  7. Git 多人协作开发的过程

    Git可以完成两件事情: 1. 版本控制 2.多人协作开发 如今的项目,规模越来越大,功能越来越多,需要有一个团队进行开发. 如果有多个开发人员共同开发一个项目,如何进行协作的呢. Git提供了一个非 ...

  8. vue Element动态设置el-menu导航当前选中项

    1,npm install vuex --save 2,在src下新建vuex文件夹,新建store.js文件: store.js import Vue from 'vue' import Vuex ...

  9. vs 的git插件

    在vs2013上Tfs提供的git管理完全无法理解他的管理方式,还是 Git Source Control Provider 好用用. 下载地址: [http://gitscc.codeplex.co ...

  10. Android 简单计算器实现源码

    1.string.xml代码 <?xml version="1.0" encoding="utf-8"?> <resources> &l ...