Zoj 3261 connections in Galaxy war (query set)

Source: Internet
Author: User
Connections in Galaxy war
Time limit:3 seconds Memory limit:32768 KB

In order to strengthen the defense ability, starting stars in Galaxy allied together and built into bidirectional tunnels to exchange messages. however, when the galaxy war began, some tunnels were destroyed by the monsters from another dimension. then then problems were raised when some of the stars wanted to seek help from the others.

In the galaxy, the stars are numbered from 0N-1 and their power was marked by a non-negative integerPi. When the starAWanted to seek help, it wocould send the message to the star with the largest power which was connected with starADirectly or indirectly. In addition, this star shoshould be more powerful than the starA. 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 starACouldn't find such star for help.

Given the information of the war and the queries about some special stars, for each query, please find out whether this star cocould seek another star for help and which star shoshould 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 Tu Nnels 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 (M+ 2)-th line is an integerQ(0 <=Q<= 50000) which is the number of the information and queries. In the followingQLines, each line will be written in one of next two formats.

"Destroy a B"-The connection between starAAnd StarBWas destroyed by the monsters. It's guaranteed that the connection between starAAnd StarBWas available before the monsters 'attack.

"Query"-StarAWanted to know which star it shoshould 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

 
210 2010 15 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

 

 

 

Question link: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 3563

Offline processing.

Add the edge. This is a simple query.

 

# Include <stdio. h># Include < String . H> # Include <Algorithm> # Include <Iostream> # Include <Map> Using   Namespace  STD;  Const   Int Maxn = 10010  ;  Int  F [maxn]; Int  P [maxn];  Int Val [maxn]; //  Subscript of the maximum value  Int Num [maxn]; //  Maximum Value  Int Find ( Int  X ){  If (F [x] =- 1 ) Return  X;  Return F [x] = Find (F [x]);}  Void Bing ( Int U, Int  V ){  Int T1 = find (u), T2 = Find (v );  If (T1! = T2) {f [T1] = T2;  If (Num [T1]> Num [T2]) {num [T2] = Num [T1]; Val [T2] =Val [T1];}  Else   If (Num [T1] = num [T2] & Val [T2]> Val [T1]) Val [T2] = Val [T1] ;}} Map < Int , Int > MP [maxn];  Struct  Edge {  Int  U, V;} edge [  20010  ]; Bool Used [ 20010  ];  Struct  Node {  Int  OP;  Int  U, V;} node [  50010  ];  Int Ans [ 50010  ];  Char STR [ 20 ];  Int  Main (){  Int  N;  Int  Q;  Int  M;  Int  U, V;  Bool First = True  ;  While (Scanf ( "  % D " , & N) = 1  ){  If (First) First = False  ;  Else Printf ( "  \ N  "  ); Memset (F, - 1 , Sizeof  (F ));  For (Int I = 0 ; I <n; I ++ ) {Scanf (  "  % D  " ,& P [I]); Val [I] = I; num [I] = P [I]; MP [I]. Clear ();} scanf (  "  % D  " ,& M );  For (Int I = 0 ; I <m; I ++ ) {Scanf (  "  % D  " , & U ,& V );  If (U> V) Swap (u, v); MP [u] [v] = I; edge [I]. u = U; edge [I]. v = V; used [I] = False ;} Scanf (  "  % D  " ,& Q );  For ( Int I = 0 ; I <q; I ++ ) {Scanf (  "  % S  " ,& Str );  If (STR [ 0 ] = '  Q  '  ) {Node [I]. OP = 0  ; Scanf (  "  % D  " ,& Node [I]. U );}  Else  {Node [I]. OP = 1  ; Scanf ( "  % D  " , & U ,& V );  If (U> V) Swap (u, v); node [I]. u = U; node [I]. v = V;  Int TMP = MP [u] [v]; used [TMP] = True  ;}}  For (Int I = 0 ; I <m; I ++ )  If (! Used [I]) {Bing (edge [I]. U, edge [I]. V );}  Int CNT = 0  ;  For ( Int I = Q- 1 ; I> = 0 ; I -- ){ If (Node [I]. Op = 0  ) {U = Node [I]. U;  Int T1 = Find (U );  If (Num [T1]> P [u]) ans [CNT ++] = Val [T1];  Else Ans [CNT ++] =- 1  ;}  Else {Bing (node [I]. U, node [I]. v );}}  For ( Int I = CNT- 1 ; I> = 0 ; I --) printf ( "  % D \ n  "  , ANS [I]);}  Return   0  ;} 

 

 

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.