ZOJ 3261 Connections in Galaxy War (also set)

Source: Internet
Author: User

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 E Xchange messages. However, when the Galaxy War began, some tunnels were destroyed by the monsters from another dimension. Then many problems were raised if some of the stars wanted to seek help from the others.

In the Galaxy, the stars is numbered from 0 to N-1 and their power is 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 Conne CTED 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 and then the one and 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 is chosen.

Input

There is no more than cases. Process to the end of file.

For each cases, the first line contains a integer  n   (1 <=  N  <= 10000), WH Ich 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 was a single integer  m   (0 <=  m  <= 20000), which is the Numbe R of tunnels built before the war. then  M  lines follows. Each line has a 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 is only being described once.

In the (M + 2)-th line is a integer Q (0 <= q <= 50000) which is the number of the infor Mation and Queries. In the following Q lines, each line would be written in one of the next formats.

"destroy a B" -the connection between star a and star B were destroyed by the monsters. It's guaranteed that the connection between star a and star B were available before the monsters ' attack.

"Query A" -star a wanted to know which star it should turn

There is a blank line between consecutive cases.

Output

For each query with the input, if there is no star a can turn to for help and then output "-1"; Otherwise, output the serial number of the chosen star.

Print a blank line between consecutive cases.

Sample Input

15query 0query 1destroy 0 1query 0query 1

Sample Output

1-1-1-1



Test instructions: There are N planet number 0~n-1, each planet has a fighting power, in Star Wars, the weak planet can turn to other than its stronger planet to help, of course, these planets require direct or indirect connection with it, give the M group of two planets, communication between two planets may be damaged by monsters. After the Q group is asked, query a indicates that the planet number A to other stronger planets to help, find a direct or indirect connection with a maximum combat effectiveness of the planet, if there are multiple, the output number of the smallest, destroy a b indicates that A and B communication is damaged.

Analysis: And check the set, all the data are saved, and then processed backwards. for questions raised: answer in a timely manner, save the answer. for deleted branches: Do and check the merger of the set.

If it is processed sequentially, the associated points are merged first, and when there is a destory operation later, it is not good to handle. So it's backwards processing, offline input data, starting from the last state, if it is the query when you do the inquiry operation, if it is destory do the merge operation. When merging, the maximum value is updated by comparing the value of the current node and the value of the root node.

#pragmaComment (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<string>#include<iostream>#include<cstring>#include<cmath>#include<stack>#include<queue>#include<vector>#include<map>#include<stdlib.h>#include<algorithm>#defineLL __int64using namespacestd;Const intmaxn=10000+5;Const intHash=10000;intn,m,q;intans[50000+5];intP[MAXN];intVal[maxn];map<int,int>Mat;structedge{intU,v;} a[20000+5];structnode{Charstr[Ten]; intU,v;} b[50000+5];voidinit () { for(intI=0; i<n;i++) p[i]=i; memset (ans,0,sizeof(ans)); Memset (Val,0,sizeof(Val)); Memset (A,0,sizeof(a)); memset (b,0,sizeof(b)); Mat.clear ();}intFINDFA (intx) {    returnp[x]==x?x:p[x]=Findfa (p[x]);}voidUnion (intUintv) {    intx=FINDFA (U); inty=Findfa (v); if(x!=y) {if(Val[x]>val[y]) p[y]=x; Else if(Val[x]<val[y]) p[x]=y; Else        {            if(x<y) p[y]=x; Elsep[x]=y; }    }}intMain () {//freopen ("In.txt", "R", stdin);    BOOLflag=false;  while(SCANF ("%d", &n)! =EOF)        {init ();  for(intI=0; i<n;i++) scanf ("%d",&Val[i]); scanf ("%d",&m);  for(intI=0; i<m;i++) {scanf ("%d%d",&a[i].u,&a[i].v); if(a[i].u>a[i].v) Swap (A[I].U,A[I].V); } scanf ("%d",&Q);  for(intI=0; i<q;i++) {scanf ("%s",&b[i].str); if(b[i].str[0]=='Q') scanf ("%d",&b[i].u); Else{scanf ("%d%d",&b[i].u,&b[i].v); if(b[i].u>b[i].v) Swap (B[I].U,B[I].V); MAT[B[I].U*hash+b[i].v]=1; }        }         for(intI=0; i<m;i++)            if(mat[a[i].u*hash+a[i].v]!=1) Union (A[I].U,A[I].V); intCnt=0;  for(inti=q-1; i>=0; i--)        {            if(b[i].str[0]=='Q')            {                intx=Findfa (B[I].U); if(Val[x]>val[b[i].u]) ans[cnt++]=x; Elseans[cnt++]=-1; }            ElseUnion (B[I].U,B[I].V); }        if(flag) printf ("\ n"); Elseflag=true;  for(inti=cnt-1; i>=0; i--) printf ("%d\n", Ans[i]); }    return 0;}
View Code

ZOJ 3261 Connections in Galaxy War (and set)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.