並查集-----L - 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 A directly 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 star A 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 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 b was 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
2
10 20
1
0 1
5
query 0
query 1
destroy 0 1
query 0
query 1
Sample Output
1
-1
-1
-1
傳統的做法是先把輸入的點加入並查集建立關係,然後開始判斷詢問,遇到destroy就將那兩個點之間的關係斷開,但是很明顯,這樣很難做到將其關係斷開。。。
對啊,正常並查集做不到怎麼辦,不要一直被侷限在正常思維,可以逆向思維想一下,那些destory的邊我先不進行合併,將詢問離線,倒著處理詢問,這樣就簡單多了。。。
怎麼找到最小標號的最大值呢,就要在合併的時候處理一下子了,只要每次優先權值小的指向權值大的,遇到權值相同的,則標號大的指向標號小的。。。。。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1);
const double eps = 1e-8;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N = 10005;
int val[N];
int b[N];
int ans[50005];
pair<int,int>ve[20005];
map<pair<int,int>,int>mk;
typedef struct Node{
char s[15];
int x,y;
}Node;
Node node[50005];
int n,m,q;
void init()
{
for(int i = 0;i < n;++i){
b[i] = i;
}
}
int join(int x)
{
if(b[x] != x){
b[x] = join(b[x]);
}
return b[x];
}
void unin(int x,int y)
{
int tx = join(x);
int ty = join(y);
if(tx != ty){
if(val[tx] < val[ty]){
b[tx] = ty;
}else if(val[tx] > val[ty]){
b[ty] = tx;
}else if(tx < ty){
b[ty] = tx;
}else{
b[tx] = ty;
}
}
}
int main()
{
bool flag = false;
while(~scanf("%d",&n))
{
init();
if(flag){
printf("\n");
}else{
flag = true;
}
mk.clear();
for(int i = 0;i < n;++i){
scanf("%d",&val[i]);
}
scanf("%d",&m);
for(int i = 0;i < m;++i){
int x,y;
scanf("%d %d",&x,&y);
ve[i].fi = x;ve[i].se = y;
}
scanf("%d",&q);
for(int i = 0;i < q;++i){
scanf("%s",node[i].s);
if(node[i].s[0] == 'q'){
scanf("%d",&node[i].x);
}else{
scanf("%d %d",&node[i].x,&node[i].y);
mk[mp(node[i].x,node[i].y)] = 1;
mk[mp(node[i].y,node[i].x)] = 1;
}
}
for(int i = 0;i < m;++i){
if(mk[mp(ve[i].fi,ve[i].se)] == 0){
unin(ve[i].fi,ve[i].se);
}
}
memset(ans,inf,sizeof(ans));
for(int i = q - 1;i >= 0;--i){
if(node[i].s[0] == 'd'){
unin(node[i].x,node[i].y);
}else{
int tx = join(node[i].x);
if(val[tx] > val[node[i].x]){
ans[i] = tx;
}else{
ans[i] = -1;
}
}
}
for(int i = 0;i < q;++i){
if(ans[i] != inf){
printf("%d\n",ans[i]);
}
}
}
return 0;
}