点击打开链接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. MyEclipse使用总结——MyEclipse文件查找技巧 ctrl+shift+R ctrl+H

    一.查找文件 使用快捷键[ctrl+shift+R]弹出弹出文件查找框,如下图所示: 二.查找包含某个字符串的文件 使用快捷键[ctrl+H]在弹出对话框中选File Search选项,然后在第一个文 ...

  2. Tomcat日志输出在linux和windows差异

    tomca日志保存在${catalina}/logs目录下: 主要包括五类文件,开发测试常使用的日志主要保存在catalina.localhost日志中,且每天生成独立日志文件,结尾以日期命名,方便查 ...

  3. IoC实践--用Unity实现MVC5.0的IoC控制反转方法

    在MVC中,控制器依赖于模型对数据进行处理,也可以说执行业务逻辑.我们可以使用依赖注入(DI)在控制层分离模型层,这边要用到Repository模式,在领域驱动设计(DDD)中,Repository翻 ...

  4. STL的string和wstring

    STL有字符串处理类——stirng和wstring,但是用的时候会觉得不是很方便,因为它不能像TCHAR一样根据定义的宏在char类型字符串和wchar_t进行转换,总不能因为程序要Unicode就 ...

  5. 转:LIRE的使用

    LIRE的使用:创建索引 LIRE(Lucene Image REtrieval)提供一种的简单方式来创建基于图像特性的Lucene索引.利用该索引就能够构建一个基于内容的图像检索(content- ...

  6. Python 3 数值计算

    Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32Type & ...

  7. 修改oracle内存占用

    修改oracle内存占用   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 # su oracle      $cd $ORACLE_HOM ...

  8. JVM常见的七种垃圾收集器的简单比较

    1.Serial收集器曾经是虚拟机新生代收集的唯一选择,是一个单线程的收集器,在进行收集垃圾时,必须stop the world,它是虚拟机运行在Client模式下的默认新生代收集器. 2.Seria ...

  9. jquery实现返回基部案例效果

    <!doctype html> <html> <head> <meta charset="gb2312"> <title> ...

  10. 源代码安装 MySQL 5.6.28

    本文内容 创建 MySQL 用户和组 解压 MySQL 源代码包 生成配置安装文件 编译和安装 MySQL 配置文件 创建 MySQL 授权表 MySQL 目录授权 启动 MySQL 验证 MySQL ...