点击打开链接zoj 3261

思路: 带权并查集
分析:
1 题目说的是有n个星球0~n-1,每个星球都有一个战斗值。n个星球之间有一些联系,并且n个星球之间会有互相伤害
2 根本没有思路的题,看了网上的思路才知道是逆向并查集。如果我们按照正常的并查集来做,以战斗值最大为根节点的话,当询问的时候很容易,但是碰到删除边的时候就很困难了,所以这里才用逆向的并查集思路
3 我们先把所有的输入保存,然后我们可以这么考虑,从后面往前面枚举q次条件,如果是destroy我们认为是加边,这样的话就很好维护并查集了
4 但是这边我们还要考虑初始的状态,由于涉及到删边而且不一定是删除所有的边,所以我们只要在m个关系里面扣除要删除的边,然后建立集合做为初始的状态

代码:

#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; const int MAXN = 50010; struct Node{
int mark;
int x;
int y;
};
Node node[MAXN];
Node edge[MAXN]; int n , m , q;
int val[MAXN];
int father[MAXN];
int ans[MAXN];
map<int , int>mp; void init(){
mp.clear();
for(int i = 0 ; i < n ; i++)
father[i] = i;
} int find(int x){
if(father[x] != x)
father[x] = find(father[x]);
return father[x];
} void Union(int x , int y){
int fx = find(x);
int fy = find(y);
if(fx != fy){
if(val[fx] > val[fy])
father[fy] = fx;
else if(val[fx] < val[fy])
father[fx] = fy;
else{
if(fx < fy)
father[fy] = fx;
else
father[fx] = fy;
}
}
} void solve(){
for(int i = 0 ; i < m ; i++){
if(mp[edge[i].x*MAXN+edge[i].y])
continue;
Union(edge[i].x , edge[i].y);
}
int pos = 0;
for(int i = q-1 ; i >= 0 ; i--){
if(node[i].mark == 0){
int fx = find(node[i].x);
// 这边不能写成的node[i].x != fx;
// 因为有可能跟节点和它的值相同
if(val[node[i].x] >= val[fx])
ans[pos++] = -1;
else
ans[pos++] = fx;
}
else
Union(node[i].x , node[i].y);
}
for(int i = pos-1 ; i >= 0 ; i--)
printf("%d\n" , ans[i]);
} int main(){
int x , y;
char str[10];
bool first = true;
while(scanf("%d" , &n) != EOF){
if(first)
first = false;
else
puts("");
for(int i = 0 ; i < n ; i++)
scanf("%d" , &val[i]);
init();
scanf("%d" , &m);
for(int i = 0 ; i < m ; i++){
scanf("%d%d" , &edge[i].x , &edge[i].y);
if(edge[i].x > edge[i].y)
swap(edge[i].x , edge[i].y);
}
scanf("%d" , &q);
for(int i = 0 ; i < q ; i++){
scanf("%s" , str);
if(str[0] == 'q'){
scanf("%d" , &node[i].x);
node[i].mark = 0;
}
else{
scanf("%d%d" , &node[i].x , &node[i].y);
if(node[i].x > node[i].y)
swap(node[i].x , node[i].y);
node[i].mark = 1;
mp[node[i].x*MAXN+node[i].y] = 1;
}
}
solve();
}
return 0;
}

zoj 3261 Connections in Galaxy War的更多相关文章

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

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

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

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

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

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

  4. zoj 3261 Connections in Galaxy War(并查集逆向加边)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 题意:有很多颗星球,各自有武力值,星球间有一些联系通道,现 ...

  5. ZOJ 3261 Connections in Galaxy War(逆向并查集)

    参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...

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

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

  7. ZOJ 3261 Connections in Galaxy War (逆向+带权并查集)

    题意:有N个星球,每个星球有自己的武力值.星球之间有M条无向边,连通的两个点可以相互呼叫支援,前提是对方的武力值要大于自己.当武力值最大的伙伴有多个时,选择编号最小的.有Q次操作,destroy为切断 ...

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

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

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

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

随机推荐

  1. python web框架——扩展Django&tornado

    一 Django自定义分页 目的:自定义分页功能,并把它写成模块(注意其中涉及到的python基础知识) models.py文件 # Create your models here. class Us ...

  2. js/jq宽高的理解与运用

    document:1. 与client相关的宽高document.body.clientWidthdocument.body.clientHeightdocument.body.clientLeftd ...

  3. android: 使用前台服务

    9.5.1    使用前台服务 服务几乎都是在后台运行的,一直以来它都是默默地做着辛苦的工作.但是服务的系统 优先级还是比较低的,当系统出现内存不足的情况时,就有可能会回收掉正在后台运行的服 务.如果 ...

  4. ARM Cortex Debug Port Access Port DP AP JTAG-DP SW-DP SWJ-DP JTAG-AP MEM-AP

  5. 构造函数模式自己定义js对象

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. Win7快捷方式图标不显示解决办法

    问题:WIN7的系统,桌面.开始菜单以及任务栏的快捷方式图标显示不正常,看不到程序默认图标,快捷方式图标不显示. 解决方法:删除程序图标缓存即可.   将下面的内容复制到记事本保存为“图标缓存清理.b ...

  7. Android按键之Menu详解

    Android手机一般都有三个键,返回键.Home键.菜单键: Android系统的菜单支持主要通过4个接口来实现. 从上图可以看出Menu是一个父类接口,它下面有两个子类一个是ContextMenu ...

  8. 使用 Express 和 waterline 创建简单 Restful API

    这几篇都是我原来首发在 segmentfault 上的地址:https://segmentfault.com/a/1190000004996659  突然想起来我这个博客冷落了好多年了,也该更新一下, ...

  9. [2013 eoe移动开发者大会]靳岩:从码农到极客的升级之路

    (国内知名Android开发论坛 eoe开发者社区推荐:http://www.eoeandroid.com/) 前天,2013 eoe 移动开发者大会在国家会议中心召开,eoe 开发者社区创始人靳岩在 ...

  10. 通过 SSH 隧道方式图形化连接 AIX 服务器

    跳转到主要内容 登录 (或注册) 中文 [userid] IBM ID: 密码: 保持登录. 单击提交则表示您同意developerWorks 的条款和条件. 查看条款和条件. 需要一个 IBM ID ...