题目链接:

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. CM和CDH的安装-进阶完成

    安装Cloudera Manager Server 和Agent 1.在cdh1解压cloudera-manager-el6-cm5.9.0_x86_64.tar.gz(cdh1节点)tar -zcv ...

  2. linux 信息收集脚本。转自insight-labs

    找出所有.sh .pl .py .conf .cnf .ini .*history .*pass* (/usr/share目录里面的除外) 并且在当前目录zip打包.有些时候很多配置文件的权限配置不严 ...

  3. U3D的控制

    做游戏少不了控制,但是一个成熟的游戏引擎,是不能简单仅仅获取键盘中或者遥感确定的按键来控制,要考虑到用户更改游戏按键的情况,当然也得考虑到不同设备的不通输入方式,比如U3D是可以运行在iphone上的 ...

  4. ip防刷脚本

    #!/bin/sh #防刷脚本 #env ACCESS_PATH=/home/wwwlogs ACCESS_LOG=y.log IPTABLES_TOP_LOG=iptables_top.log DR ...

  5. smarty模板文件书写javascript代码

    在smarty文件里直接写javascript代码时候,造成500错误. javascript代码有很多的{}在同一行,而{}也是smarty引擎解析模板的关键标识符,smarty将对其进行分析,这时 ...

  6. linux下命令学习

    1 在linux中,./代表当前目录下 例如 创建一个文件夹123   mkdir ./123   ->当前目录下创建一个123文件夹 mkdir -p ./123/456  在当前目录下创建一 ...

  7. leetCode练习题

    1.求二叉树的最小深度: public class Solution { public int run(TreeNode root) { if(root==null) return 0; int l ...

  8. 【CSS系列】获取实时数据做进度

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. GitHub 在使用命令行 git push 时报错:The requested URL returned error: 403

    使用 git 的命令行向 GitHub 提交的时候,报错: [Young@localhost OtherLang]$ git push origin master error: The request ...

  10. Oracle 学习之 Select 1

    1. select 1 from table           增加临时列,每行的列值是写在select后的数,这条sql语句中是1,若select后为2,则是每行为2的列 2:select cou ...