Connections in Galaxy War (逆向并查集)题解
Connections in Galaxy War
In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional tunnels to exchange messages. However, when the Galaxy War began, some tunnels were destroyed by the monsters from another dimension. Then many problems were raised when some of the stars wanted to seek help from the others.
In the galaxy, the stars are numbered from 0 to N-1 and their power was marked by a non-negative integer pi. When the star A wanted to seek help, it would send the message to the star with the largest power which was connected with star Adirectly or indirectly. In addition, this star should be more powerful than the star A. If there were more than one star which had the same largest power, then the one with the smallest serial number was chosen. And therefore, sometimes starA couldn't find such star for help.
Given the information of the war and the queries about some particular stars, for each query, please find out whether this star could seek another star for help and which star should be chosen.
There are no more than 20 cases. Process to the end of file.
For each cases, the first line contains an integer N (1 <= N <= 10000), which is the number of stars. The second line contains N integers p0, p1, ... , pn-1 (0 <=pi <= 1000000000), representing the power of the i-th star. Then the third line is a single integer M (0 <= M <= 20000), that is the number of tunnels built before the war. Then M lines follows. Each line has two integers a, b (0 <= a, b<= N - 1, a != b), which means star a and star b has a connection tunnel. It's guaranteed that each connection will only be described once.
In the (M + 2)-th line is an integer Q (0 <= Q <= 50000) which is the number of the information and queries. In the following Q lines, each line will be written in one of next two formats.
"destroy a b" - the connection between star a and star b was destroyed by the monsters. It's guaranteed that the connection between star a and star bwas available before the monsters' attack.
"query a" - star a wanted to know which star it should turn to for help
There is a blank line between consecutive cases.
For each query in the input, if there is no star that star a can turn to for help, then output "-1"; otherwise, output the serial number of the chosen star.
Print a blank line between consecutive cases.
2
10 20
1
0 1
5
query 0
query 1
destroy 0 1
query 0
query 1
1
-1
-1
-1
思路:并查集一般是建立连接,在断连接时比较困难,故想到用逆向方法,先记录指令,再建立没有destroy的指令,再逆向查看指令,遇到destroy再建立连接
这里用到了以前没有用到过的map<int,bool>mp;可以看做超大数组,需要#include<map>和using namespace std;
之前用循环查找最佳求救星球超时了,这里用了join()直接把根当成最佳
这里研究了某大佬代码:某大佬题解
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<map>
#define N 10010
#define HASH 10000
using namespace std;
int pre[N];
char s[50010][10];
long long int p[N];
map<int,bool>mp;
int find(int a){
	int r=a;
	while(pre[r]!=r){
		r=pre[r];
	}
	int i=a,j;
	while(pre[i]!=i){
		j=pre[i];
		pre[i]=r;
		i=j;
	}
	return r;
}
void join(int x,int y)	//保证根永远是求救的最佳人选
{
    int fx=find(x),fy=find(y);
    if(fx!=fy)
    {
        if(p[fx]>p[fy])
            pre[fy]=fx;
        else if(p[fx]<p[fy])
            pre[fx]=fy;
        else
        {
            if(fx<fy)
                pre[fy]=fx;
            else
                pre[fx]=fy;
        }
    }
}
int main(){
	int i,n,m,q,j,a[50010],b[50010],c[50010],d[50010],answer[50010],help,all,first=0,t;
	while(scanf("%d",&n)!=EOF){
		if(first)	printf("\n");
		first=1;
		for(i=0;i<n;i++){
			scanf("%lld",&p[i]);
			pre[i]=i;
		}
		scanf("%d",&m);
		for(i=0;i<m;i++){
			scanf("%d%d",&a[i],&b[i]);
			if(a[i]>b[i]){
				t=a[i];
				a[i]=b[i];
				b[i]=t;
			}
		}
		mp.clear();
		scanf("%d",&q);
		for(i=0;i<q;i++){
			scanf("%s",s[i]);
			if(s[i][0]=='d'){
				scanf("%d%d",&c[i],&d[i]);
				if(c[i]>d[i]){
					t=c[i];
					c[i]=d[i];
					d[i]=t;
				}
				mp[c[i]*HASH+d[i]]=1;	//mp指示该指令是否destroy
			}
			else	scanf("%d",&c[i]);
		}
		for(i=0;i<m;i++){
			if(mp[a[i]*HASH+b[i]]==1){
				continue;
			}
			join(a[i],b[i]);
		}
		all=0;
		for(i=q-1;i>=0;i--){
			if(s[i][0]=='q'){
				help=find(c[i]);
				if(p[help]>p[c[i]])	answer[all++]=help;
				else	answer[all++]=-1;
			}
			else{
				join(c[i],d[i]);
			}
		}
		all-=1;
		for(i=all;i>=0;i--){
			printf("%d\n",answer[i]);
		}
	}
	return 0;
}Connections in Galaxy War (逆向并查集)题解的更多相关文章
- ZOJ3261:Connections in Galaxy War(逆向并查集)
		Connections in Galaxy War Time Limit: 3 Seconds Memory Limit: 32768 KB 题目链接:http://acm.zju.edu. ... 
- ZOJ 3261 Connections in Galaxy War(逆向并查集)
		参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ... 
- Connections in Galaxy War(逆向并查集)
		Connections in Galaxy War http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563 Time Limit ... 
- zoj 3261 Connections in Galaxy War(并查集逆向加边)
		题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 题意:有很多颗星球,各自有武力值,星球间有一些联系通道,现 ... 
- ZOJ3261 Connections in Galaxy War —— 反向并查集
		题目链接:https://vjudge.net/problem/ZOJ-3261 In order to strengthen the defense ability, many stars in g ... 
- ZOJ 3261 - Connections in Galaxy War ,并查集删边
		In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ... 
- ZOJ - 3261 Connections in Galaxy War(并查集删边)
		https://cn.vjudge.net/problem/ZOJ-3261 题意 银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可 ... 
- ZOJ 3261 Connections in Galaxy War (逆向+带权并查集)
		题意:有N个星球,每个星球有自己的武力值.星球之间有M条无向边,连通的两个点可以相互呼叫支援,前提是对方的武力值要大于自己.当武力值最大的伙伴有多个时,选择编号最小的.有Q次操作,destroy为切断 ... 
- ZOJ3261-Connections in Galaxy War-(逆向并查集+离线处理)
		题意: 1.有n个星球,每个星球有一个编号(1-n)和一个能量值. 2.一开始将某些星球连通. 3.开战后有很多个操作,查询某个星球能找谁求救或者摧毁两颗星球之间的连通路径,使其不能连通.如果连通则可 ... 
随机推荐
- Unity shader学习之屏幕后期处理效果之Bloom效果
			Bloom特效是游戏中常见的一种屏幕效果.这种特效可以模拟真实摄像机的一种图像效果,它让画面中较亮的区域“扩散”到周围的区域中,造成一种朦胧的效果. Bloom的实现原理很简单,首先根据一个阈值提取出 ... 
- Discuz! 安装模板、插件提示“对不起,您安装的不是正版应用...
			iscuz 社区在更新到2.0以上后,增加了对插件的版本检测,在安装时,可能会出现:“对不起,您安装的不是正版应用,安装程序无法继续执行”的提示,要解决这个其实挺容易的,找到以下文件: /source ... 
- django migrate无效的解决方法
			遇到一个很奇怪的问题 python manage.py makemigrations 的时候显示要创建两张表,但是执行 python manage.py migrate 的时候不能识别,也就是说失效了 ... 
- 【Linux学习七】软件安装
			环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 一.编译安装1.解压 源码文件是压缩包 要先解压tar -zxvf t ... 
- 关于SQL语句中的distinct和group by
			两种都能实现去重功能.区别: distinct只是将重复的行从结果中出去: group by是按指定的列分组,一般这时在select中会用到聚合函数. distinct是把不同的记录显示出来 grou ... 
- loadRunner回访脚本时报Error -27987: Requested image not found  	[MsgId: MERR-27987]
			loadRunner录制:登陆订机票网址->订机票的过程 loadRunner回访脚本时报Error -27987: Requested image not found [MsgId: MER ... 
- Linux基础命令---显示域名ypdomainname
			ypdomainname ypdomainname指令显示由函数“getdomainname”返回的主机域名,使用这个指令也可以设置一个主机NIS/YP域名. 此命令的适用范围:RedHat.RH ... 
- maven 入门 (一)
			纠结了好久,到底要不要写一份maven入门的所谓“教程”,有好几次想写一下,但是都放弃了,因为网上的太多了,而且都很好,但是现在决定了,还是要写出来,因为者有我自己的理解.所以我想写一份教程出来. 首 ... 
- 底层代码创建GUI
			%底层代码创建GUI hf = figure(... 'Units','Normalized',... 'Color','w',... 'Position',[0.1 0.1 0.8 0.8]); h ... 
- Java常考面试题(一)
			https://blog.csdn.net/linzhiqiang0316/article/details/80473906 
