点击打开链接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. oracle sqlserver mysql数据库分页

    1.Mysql的limit用法 在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,这个时候怎么办呢?不用担心,mysql已经为我们提供了这样一个功能. SELECT * FROM tabl ...

  2. python继承

    Python继承 继承实例: 父类和子类的关系: 继承树: 没有父类就继承object类,不要忘记调用super().__init__来初始化父类 代码: class Person(object): ...

  3. Java中Atomic包的实现原理及应用

    1. 同步问题的提出 假设我们使用一个双核处理器执行A和B两个线程,核1执行A线程,而核2执行B线程,这两个线程现在都要对名为obj的对象的成员变量i进行加1操作,假设i的初始值为0,理论上两个线程运 ...

  4. jmap命令详解(转)

    1.命令基本概述 Jmap是一个可以输出所有内存中对象的工具,甚至可以将VM 中的heap,以二进制输出成文本.打印出某个java进程(使用pid)内存内的,所有‘对象’的情况(如:产生那些对象,及其 ...

  5. git在分支上创建目录和文件

    创建一个空目录,在其中初始化git git init 创建一个新文件,此时默认在master分支上 touch file1.txt add到staging area git add file1.txt ...

  6. 关于ScrollerView的一些小心得

    在项目开发时遇到一个问题,我在UIViewController上面直接创建了一个UIScrollerView,把UIScrollerView作为一个子视图添加到了UIViewController, 又 ...

  7. ASP.NET弹出显示ex.Message异常信息 存在换行符和回车符处理办法。

    1.把ex.Message换成任意字符串,检验在catch语句块中可以用Response.Write方法显示对话框.结果显示成功,说明问题就出在ex.Message上. 2.在程序中下断点,可以看到e ...

  8. ubuntu下取代ping的好工具tcpping

    $ sudo apt-get install tcptraceroute bc$ cd /usr/bin$ sudo wget http://www.vdberg.org/~richard/tcppi ...

  9. Oracle 一次生产分库,升级,迁移

    今天完成了一个负载较高的中央数据库的分库操作, 并实现了oracle的滚动升级(10.2.0.1->10.2.0.4), 业务中断仅15分钟. 平台: RHEL AS 4 + Oracle 10 ...

  10. Windows内核安全与驱动开发

    这篇是计算机中Windows Mobile/Symbian类的优质预售推荐<Windows内核安全与驱动开发>. 编辑推荐 本书适合计算机安全软件从业人员.计算机相关专业院校学生以及有一定 ...