点击打开链接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. javaweb学习总结(十)——HttpServletRequest对象(一)

    一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...

  2. FreeCodeCamp 高级算法(个人向)

    freecodecamp 高级算法地址戳这里. freecodecamp的初级和中级算法,基本给个思路就能完成,而高级算法稍微麻烦了一点,所以我会把自己的解答思路写清楚,如果有错误或者更好的解法,欢迎 ...

  3. 在ArcGIS空间数据库中增加点数据的方法

    1.新建一个mxd(ArcMAP)文件 2.从ArcCatalog中把要编辑的图层拖到ArcMAP中 3.从ArcCatalog中拖一个参照图层到ArcMAP中,比如临沂市的县级区划图 4.打开Edi ...

  4. JS 清除字符串数组中,重复元素

    <script language="JavaScript"> <!-- var arrData=new Array(); for(var i=0; i<10 ...

  5. Zabbix监控Windows事件日志

    1.zabbix_agentd.win文件修改: LogFile=c:\zabbix\zabbix_agentd.log Server=1.16.2.4 ServerActive=1.16.2.4 H ...

  6. XMPP系列1:简介

    通俗解释其实XMPP 是一种很类似于http协议的一种数据传输协议,它的过程就如同“解包装--〉包装”的过程,用户只需要明白它接收的类型,并理解它返回的类型,就可以很好的利用xmpp来进行数据通讯.系 ...

  7. How to apply Local Group Policy settings silently using the ImportRegPol.exe and Apply_LGPO_Delta.exe utilities.

    参考:http://supportishere.com/how-to-apply-local-group-policy-settings-silently-using-the-importregpol ...

  8. C# 中 KeyPress 、KeyDown 和KeyPress的详细区别[转]

    研究了一下KeyDown,KeyPress 和KeyUp ,发现之间还是有点学问的. 让我们带着如下问题来说明,如果你看到这些问题你都知道,那么这篇文章你就当复习吧:) 1.这三个事件的顺序是怎么样的 ...

  9. Spark RDD解密

    1.  基于数据集的处理: 从物理存储上加载数据,然后操作数据,然后写入数据到物理设备; 基于数据集的操作不适应的场景: 不适合于大量的迭代: 不适合交互式查询:每次查询都需要对磁盘进行交互. 基于数 ...

  10. OGG-01232 Receive TCP params error: TCP/IP error 104 (Connection reset by peer), endpoint:

    源端: 2015-02-05 17:45:49 INFO OGG-01815 Virtual Memory Facilities for: COM anon alloc: mmap(MAP_ANON) ...