Otoci
Time Limit:1 Sec
Memory limit:256 MB
Topic Connection http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18141
Description
Some time ago Mirko founded a new tourist agency named "Dreams of Ice". The agency purchased N Icy islands near the South Pole and now offers excursions. Especially popular is the emperor penguins, which can be found in large numbers on the islands.
Mirko ' s agency have become a huge hit; So big that it's no longer cost-effective to use boats for the excursions. The agency would build bridges between islands and transport tourists by buses. Mirko wants to introduce a computer program to manage the bridge building process so that fewer mistakes is made.
The islands is numbered 1 through N. No. Islands is initially connected by bridges. The initial number of penguins is known. That number is change, but would always be between 0 and inclusive.
Your program must handle the following three types of commands:
- "Bridge a B" –an offer is received to build A bridge between islands A and B (A and B would be different). To limit costs, your program must accept the offer if there isn ' t already a. Sing previously built bridges. If The offer was accepted, the program should output "yes" and after which the bridge was built. If The offer was rejected, the program should output "no".
- "Penguins a X", Haven penguins on island A has been recounted and there is now X of them. This is a informative command and your program does not need to respond.
- "Excursion a B" –a group of tourists wants an excursion from island A to island B. If the excursion is possible (it's possible to get from island A to B), the program should output the total number of pen Guins the tourists would see on the excursion (including Islands A and B). Otherwise, your program should output "impossible".
Input
The first line contains the integer N (1≤n≤30), the number of islands.
The second line contains N integers between 0 and +, the initial number of penguins on each of the islands.
The third line contains an integer Q (1≤q≤300), the number of commands.
Q commands follow, each on its own line.
Output
Output the responses to Commands "Bridge" and "excursion" with each of its own line.
Sample Input
5
4 2 4) 5 6
10
Excursion 1 1
Excursion 1 2
Bridge 1 2
Excursion 1 2
Bridge 3 4
Bridge 3 5
Excursion 4 5
Bridge 1 3
Excursion 2 4
Excursion 2 5
Sample Output
4
Impossible
Yes
6
Yes
Yes
15
Yes
15
16
HINT
Test instructions
Let you maintain a tree
Link operation, update operation, point right on the query chain, and
Exercises
It's the basic operation of LCT.
This is the main maintenance of the update information inside
Code:
//Qscqesze#pragmaComment (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<bitset>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 1205000#defineMoD 1000000007#defineEPS 1e-9#definee exp (1.0)#definePI ACOs (-1)#defineLowbit (x) (x) & (-X)Const DoubleEP = 1e-Ten ;intNum;//const int INF=0X7FFFFFFF;Constll inf=999999999; inline ll read () {ll x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//*************************************************************************************Const intMAXN =100010;structNode {node*ch[2], *p;intsize, value; intW; BOOLRev; Node (intt =0); InlineBOOLDirvoid) {returnp->ch[1] == This;} InlinevoidSETC (Node *x,BOOLd) {Ch[d]= x; X->p = This; } InlinevoidRev (void) {Swap (ch[0], ch[1]); Rev ^=1; } InlinevoidPush (void) { if(rev) {ch[0]->Rev (); ch[1]->Rev (); Rev=0; }} inlinevoidUpdate (void) {Value= w+ch[0]->value + ch[1]->value; Size= ch[0]->size + ch[1]->size +1; }}tnull,*NULL= &tnull, *FIM[MAXN];//to remember the extra update null informationNode::node (int_value) {ch[0] = ch[1] = P =NULL; Rev =0; w = value =_value;} InlineBOOLIsRoot (Node *x) {returnX->p = =NULL|| (x! = x->p->ch[0] && x! = x->p->ch[1]);} InlinevoidRotate (Node *x) {Node*p = x->p;BOOLD = x->dir (); P->push (); X->Push (); if(!isroot (P)) p->p->setc (x, P->dir ());ElseX->p = p->p; P->SETC (x->ch[!d], D); X->SETC (P,!d); P-Update ();} InlinevoidSplay (Node *x) {x-Push (); while(!isRoot (x)) { if(IsRoot (x->p)) rotate (x); Else { if(X->dir () = = X->p->dir ()) {Rotate (x->p); Rotate (x);} Else{rotate (x); rotate (x);} }} x-Update ();} Inline Node* Access (Node *x) {Node*t = x, *q =NULL; for(; X! =NULL; x = x->p) {splay (x); x->ch[1] = q; Q =x; } splay (t); //Info'll be updated in the splay; returnQ;} InlinevoidEvert (Node *x) {Access (x); x-Rev ();} InlinevoidLink (node *x, node *y) {Evert (x); x->p =y;} Inline Node* Getroot (Node *x) {Node*tmp =x; Access (x); while(Tmp->push (), tmp->ch[0] !=NULL) TMP = tmp->ch[0]; Splay (TMP); returntmp;}//Be sure to have an edge between x and YInlinevoidCut (node *x, node *y) {Access (x); splay (y); if(Y->p! =x) Swap (x, y); Access (x); Splay (y); Y->p =NULL;} Inline Node* GetPath (node *x, node *y) {Evert (x); Access (y); returny;} InlinevoidClearvoid) { NULL->rev =0;NULL->size =0;NULL->value =0;}intMain () {intn=read (); for(intI=1; i<=n;i++) { intx =read (); Fim[i]=NewNode (x); } intQ =read (); Chars[ -]; while(q--) {scanf ("%s", s); if(s[0]=='e') { intX=read (), y=read (); if(Getroot (fim[x])! =Getroot (Fim[y])) {printf ("impossible\n");Continue; } Evert (Fim[x]); Access (Fim[y]); Splay (Fim[y]); printf ("%d\n",fim[y]->value); } if(s[0]=='b') { intx=read (); inty=read (); if(Getroot (fim[x]) = =Getroot (Fim[y]) puts ("No"); Else{puts ("Yes"); Link (fim[x],fim[y]); } } if(s[0]=='P') { intX=read (), y=read (); Evert (Fim[x]); Fim[x]->w =y; FIM[X]-Update (); } }}
Spoj-otoci LCT