http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563
Connections in Galaxy War
Time Limit: 3 Seconds Memory Limit: 32768 KB
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
Author: MO, Luyi
Source: ZOJ Monthly, November 2009
题意:不同的星球之间有通道,接下来给出q次操作,要么炸掉两个星球之间的通道,要么袭击某个星球。被袭击的星球将会发起求助,向他能到达的战力最高的星球发起求助(可以间接到达),并输出。
分析:两个星球之间的关系不就是并查集么,用带权并查集维护个最大战力,这还行。但是他要炸通道,我想了半天都没能有什么思路。后来才发现,我可以离线保存,反向操作!
这样一来,那么炸通道不就变成了建通道了么,突然豁然开朗!本来的集合拆分就变成了,集合合并,一个带权并查集搞得妥妥的!
WA点:对于并查集的维护的时候,注意一下优先级的问题。
真的是一道非常不错的题啊,肥肠爽!
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long int
#define INF 0x3f3f3f3f
#define Irish_Moonshine main
const int maxn = 5e4 + 2333;
int fa[maxn], n, m, a[maxn], ans[maxn];
int l1[maxn], l2[maxn], y[maxn], z[maxn];
bool x[maxn]; int head[maxn];
char s[10];
int sz;
struct node{
int u, v, c, nx;
}e[maxn];
void add(int u, int v) {
e[sz].nx = head[u];
head[u] = sz;
e[sz++].v = v;
//e[sz++].c = c;
}
void init() {
for (int i = 0; i < n; i++) fa[i] = i;
}
int find(int x) {
int r = x, t;
while (r != fa[r]) r = fa[r];
while (x != r) {
t = fa[x];
fa[x] = r;
x = t;
}
return r;
}
int Irish_Moonshine()
{
int cs = 0;
while (~scanf("%d", &n)) {
if (cs++) printf("\n");
sz = 0;
memset(head, -1, sizeof head);
memset(x, 0, sizeof x);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
init();
scanf("%d", &m);
for (int i = 1; i <= m; i++) scanf("%d%d", &l1[i], &l2[i]);
int q;
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
scanf("%s", s);
if (s[0] == 'q') {
x[i] = 0;
scanf("%d", &y[i]);
}
else {
x[i] = 1;
scanf("%d%d", &y[i], &z[i]);
add(y[i], z[i]);
add(z[i], y[i]);
}
}
bool flag;
for (int i = 1; i <= m; i++) {
flag = 1;
for (int j = head[l1[i]]; ~j; j = e[j].nx) {
if (e[j].v == l2[i]) {
flag = 0; break;
}
}
if (!flag) continue;//built
int x1 = find(l1[i]), y1 = find(l2[i]);
if (x1 != y1) {
if (a[x1] > a[y1]) fa[y1] = x1;//return c >
else if (a[x1] < a[y1]) fa[x1] = y1;
else if (x1 < y1) fa[y1] = x1;//return id <
else fa[x1] = y1;
}
}
int cnt=0;
for (int i = q; i >= 1; i--) {
if (x[i]) {
int x1 = find(y[i]), y1 = find(z[i]);
if (x1 != y1) {
if (a[x1] > a[y1]) fa[y1] = x1;//return c >
else if (a[x1] < a[y1]) fa[x1] = y1;
else if (x1 < y1) fa[y1] = x1;//return id <
else fa[x1] = y1;
}
}
else {
int x1 = find(y[i]);
ans[++cnt] = a[x1] > a[y[i]] ? x1 : -1;
}
}
for (int i = cnt; i >= 1; i--) {
printf("%d\n", ans[i]);
}
}
return 0;
}