Link:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261
Topic:
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 to E Xchange messages. However, when the Galaxy War began, some tunnels were destroyed by the monsters from another. 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 is marked by a non-negative integer pi. When the star A wanted to seek help, it would send the "the" to "star with" largest power which is connected with Star A directly or indirectly. In addition, this star should is 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 is 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. Star could seek another star for help and which star should to be chosen.
Input
There are no more than cases. Process to the end of file.
For each cases, the 1 <= n <= 10000), the ' The ' the ' contains ', 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 s Tar. Then The third line is a single integer m (0 <= M <= 20000), which is the number of tunnels built 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 would be described once.
In the (M + 2)-th line is a integer Q (0 <= Q <= 50000) which is the number of the information and queries. In the following Q lines, each line would be written in one of the next two formats.
"Destroy a B"-the connection between Star A and star B is destroyed by the monsters. It's guaranteed that's 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
There is a blank line between consecutive cases.
Output
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
For each query in the input, if there is no star so star a can turn to as 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
The main effect of the topic:
The stars of the Milky Way have different energy values, and they are connected to each other and can be used to transmit information so that once a planet is attacked by monsters, it can help by finding the planet with the greatest energy value through the passage. But some passages have been destroyed by monsters.
Now give all of the original channels, then ask, ask for two ways:
Destroy a B: the channel connecting the A,b was destroyed by the beast.
Query A: Ask if a can find help through the channel.
Analysis and Summary:
If you follow the inertial thinking, first use and search the set to connect all the channels, and then delete the edge, then I am afraid there is no feasibility.
An ingenious approach is that we can follow the reverse thinking, enter all the input off-line, then connect the broken channel, then push from the last question to the front, and then union the a,b when there is a destroy a B.
Code:
#include <iostream> #include <cstdio> #include <map> using namespace std;
const int HASH = 10000;
const int N = 10005;
const int M = 20005;
Map<int, bool>mp;
int f[n];
int rank[n];
int n,m,q; int ans[50005];
Save answer struct buit{int u, v;
}BUILT[M];
struct query{char cmd[10];
int A, B;
}QUERY[50005];
void Make_set () {for (int i=0; i<n; ++i) f[i]=i;
int find (int x) {int i,j=x;
while (J!=f[j]) j=f[j];
while (X!=J) {i=f[x]; f[x]=j x=i;}
Return J;
} void Union (int x,int y) {int a=find (x), B=find (y);;
if (a==b) return;
if (rank[a]>rank[b]) f[b]=a;
else if (rank[a]<rank[b]) f[a]=b;
else{if (a>b) f[a]=b;
else f[b]=a;
int main () {bool flag=false;
while (~SCANF ("%d", &n)) {for (int i=0; i<n; ++i) scanf ("%d", &rank[i]); scanf ("%D ", &m);
for (int i=0; i<m; ++i) {scanf ("%d%d", &built[i].u, &BUILT[I].V);
if (BUILT[I].U>BUILT[I].V) {swap (built[i].u, BUILT[I].V);
} scanf ("%d", &q);
Mp.clear ();
for (int i=0; i<q; ++i) {scanf ("%s", query[i].cmd);
if (query[i].cmd[0]== ' Q ') scanf ("%d", &query[i].a);
else{scanf ("%d%d", &query[i].a,&query[i].b);
if (query[i].a>query[i].b) swap (QUERY[I].A,QUERY[I].B);
Mp[query[i].a*hash+query[i].b]=true;;
}//Merge no deleted Edge Make_set ();
for (int i=0; i<m; ++i) if (!MP[BUILT[I].U*HASH+BUILT[I].V)) {Union (built[i].u, BUILT[I].V);
//ask int k=0; for (int i=q-1; i>=0;-i) {if (query[i].cmd[0]== ' Q ') {int fa=find (query[i).a);
if (rank[fa]>rank[query[i].a]) Ans[k++]=fa;
else Ans[k++]=-1;
} else{Union (query[i].a,query[i].b);
} if (flag) puts ("");
else flag=true;
for (int i=k-1; i>=0;-i) printf ("%d\n", ans[i));
return 0; }