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
题意:
在一个图中,有两种操作,第一种删除某条边,第二种询问某个点所在联通块中权值最大的点。
思路:维护一个带权并查集,因为普通并查集不带有删边操作,所以可以离线所有的询问,然后从后往前处理操作,这样删边操作就变成了连边。
1 #include <iostream> 2 #include <fstream> 3 #include <sstream> 4 #include <cstdlib> 5 #include <cstdio> 6 #include <cmath> 7 #include <string> 8 #include <cstring> 9 #include <algorithm> 10 #include <queue> 11 #include <stack> 12 #include <vector> 13 #include <set> 14 #include <map> 15 #include <list> 16 #include <iomanip> 17 #include <cctype> 18 #include <cassert> 19 #include <bitset> 20 #include <ctime> 21 22 using namespace std; 23 24 #define pau system("pause") 25 #define ll long long 26 #define pii pair<int, int> 27 #define pb push_back 28 #define mp make_pair 29 #define clr(a, x) memset(a, x, sizeof(a)) 30 31 const double pi = acos(-1.0); 32 const int INF = 0x3f3f3f3f; 33 const int MOD = 1e9 + 7; 34 const double EPS = 1e-9; 35 36 /* 37 #include <ext/pb_ds/assoc_container.hpp> 38 #include <ext/pb_ds/tree_policy.hpp> 39 40 using namespace __gnu_pbds; 41 tree<pli, null_type, greater<pli>, rb_tree_tag, tree_order_statistics_node_update> T; 42 */ 43 44 int Rank[10015], parent[10015], root[10015], n, m, q, w[10015], fi; 45 struct query { 46 int op, x, y; 47 } Q[50015]; 48 stack<int> ans; 49 vector<pii> E[10015]; 50 void ini() { 51 for (int i = 0; i < n; ++i) { 52 parent[i] = root[i] = i; 53 Rank[i] = 1; 54 E[i].clear(); 55 } 56 } 57 int Find(int x) {return x == parent[x] ? x : parent[x] = Find(parent[x]);} 58 void uni(int x, int y) { 59 x = Find(x), y = Find(y); 60 if (x == y) return; 61 62 if (Rank[x] < Rank[y]) { 63 parent[x] = y; 64 } else { 65 parent[y] = x; 66 if (Rank[x] == Rank[y]) ++Rank[x]; 67 } 68 int rx = root[x], ry = root[y]; 69 if (w[rx] > w[ry] || (w[rx] == w[ry] && rx < ry)) { 70 root[y] = rx; 71 } else { 72 root[x] = ry; 73 } 74 } 75 int main() { 76 while (~scanf("%d", &n)) { 77 if (fi) puts(""); 78 else fi = 1; 79 ini(); 80 for (int i = 0; i < n; ++i) scanf("%d", &w[i]); 81 scanf("%d", &m); 82 for (int i = 1; i <= m; ++i) { 83 int a, b; 84 scanf("%d%d", &a, &b); 85 if (a > b) swap(a, b); 86 E[a].pb(pii(b, 1)); 87 } 88 for (int i = 0; i < n; ++i) { 89 sort(E[i].begin(), E[i].end()); 90 } 91 scanf("%d", &q); 92 for (int i = 1; i <= q; ++i) { 93 char s[11]; 94 scanf("%s", s + 1); 95 if ('q' == s[1]) { 96 Q[i].op = 1; 97 scanf("%d", &Q[i].x); 98 Q[i].y = 0; 99 } else { 100 Q[i].op = 2; 101 scanf("%d%d", &Q[i].x, &Q[i].y); 102 int a = Q[i].x, b = Q[i].y; 103 if (a > b) swap(a, b); 104 int p = lower_bound(E[a].begin(), E[a].end(), pii(b, 0)) - E[a].begin(); 105 E[a][p].second = 0; 106 } 107 } 108 for (int i = 0; i < n; ++i) { 109 for (int j = 0; j < E[i].size(); ++j) { 110 int x = E[i][j].first, f = E[i][j].second; 111 if (f) uni(i, x); 112 } 113 } 114 for (int i = q; i; --i) { 115 int op = Q[i].op, x = Q[i].x, y = Q[i].y; 116 if (1 == op) { 117 int rx = Find(x); 118 ans.push(w[root[rx]] <= w[x] ? -1 : root[rx]); 119 } else { 120 uni(x, y); 121 } 122 } 123 while (ans.size()) { 124 int x = ans.top(); 125 ans.pop(); 126 printf("%d ", x); 127 } 128 } 129 return 0; 130 }