博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Connections in Galaxy War (逆向并查集)题解
阅读量:4654 次
发布时间:2019-06-09

本文共 3607 字,大约阅读时间需要 12 分钟。

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.

Input

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 p0p1, ... , 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 ab (0 <= ab<= 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.

Output

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.

Sample Input

210 2010 15query 0query 1destroy 0 1query 0query 1

Sample Output

1-1-1-1

思路:并查集一般是建立连接,在断连接时比较困难,故想到用逆向方法,先记录指令,再建立没有destroy的指令,再逆向查看指令,遇到destroy再建立连接

这里用到了以前没有用到过的map<int,bool>mp;可以看做超大数组,需要#include<map>和using namespace std;

之前用循环查找最佳求救星球超时了,这里用了join()直接把根当成最佳

这里研究了某大佬代码:

#include
#include
#include
#include
#include
#define N 10010#define HASH 10000using namespace std;int pre[N];char s[50010][10];long long int p[N];map
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]
b[i]){ t=a[i]; a[i]=b[i]; b[i]=t; } } mp.clear(); scanf("%d",&q); for(i=0;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
=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;}

转载于:https://www.cnblogs.com/KirinSB/p/9409141.html

你可能感兴趣的文章
转载:javaweb学习总结(三十)——EL函数库
查看>>
用matplotlib库画图
查看>>
读完这篇文章,再决定做不做博后吧
查看>>
JS实现异步编程的几种方式
查看>>
js生成验证码并验证
查看>>
【Java/Android性能优5】 Android ImageCache图片缓存,使用简单,支持预取,支持多种缓存算法,支持不同网络类型,扩展性强...
查看>>
神奇01串
查看>>
2017-9-8-Linux下VNC server开启&图形界面显示
查看>>
CORS权限控制
查看>>
汽车座舱域控制器
查看>>
执行ajax加载页面中的js总结
查看>>
UITextField的使用
查看>>
MiniGUI - 列表型控件
查看>>
gitignore / Delphi.gitignore
查看>>
大水题
查看>>
2.11-2.12 HBase的数据迁移常见方式
查看>>
C#6.0特性(快来围观)(转)
查看>>
写给future的话
查看>>
决策树算法梳理
查看>>
centos rpm包下载地址
查看>>