题目链接:

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. Elasticsearch未授权访问漏洞

    Elasticsearch服务普遍存在一个未授权访问的问题,攻击者通常可以请求一个开放9200或9300的服务器进行恶意攻击. 0x00 Elasticsearch 安装 前提,保证安装了JDK 1. ...

  2. 如何使用Web Service新建和更新Wiki页面的内容

    公司内部有很多部门都创建了Wiki库,来做知识共享.公司是Hosting的SharePoint环境,不能写服务器端代码,要操作Wiki只能通过Web Service来完成,所以,碰到两个情况: 1)W ...

  3. 客户端远程连接linux下mysql数据库授权

    mysql默认状态是只支持localhost连接,这样远程服务器都输入IP地址去连接你的服务器是不可以的,下面我来介绍怎么让mysql允许远程连接配置方法,有需要的朋友可参考.   方法一,直接利用p ...

  4. 微信小程序实例源码大全2

    wx-gesture-lock  微信小程序的手势密码 WXCustomSwitch 微信小程序自定义 Switch 组件模板 WeixinAppBdNovel 微信小程序demo:百度小说搜索 sh ...

  5. 【面试题】源石智影科技Python工程师笔试题

    哈哈 上图

  6. 【PHP】快递鸟 物流查询接口实现

    官方网址: http://www.kdniao.com 即时查询api: http://www.kdniao.com/api-track 需要登录 ,申请一下 用户ID 和 API key 代码实现: ...

  7. 【linux系列】压缩和解压缩tar

    tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...

  8. wex5 onactive不执行的解决办法

    由index.w点击某个图片,转到adetail,希望每次adetail加载时,取到参数id index.w <tbody class="x-list-template" x ...

  9. linux下php redis扩展安装

    sudo tar vxzf redis-2.2.7.tgz cd redis-2.2.7 执行sudo /data/service/php54/bin/phpize 在目录下生成配置文件 sudo . ...

  10. 5-4 import,export属性

    一.默认 export default 匿名的方法 这种导出的方式不需要知道变量的名字, 相当于是匿名的, 直接把开发的接口给export:如果一个js模块文件就只有一个功能, 那么就可以使用expo ...