2843: Polar travel time limit:10 Sec Memory limit:256 MB
Submit:90 solved:56
[Submit] [Status] Description not long ago, Mirko established a travel agency called "Polar Dream". The travel agency buys N Iceland near the Arctic and provides sightseeing services. The local most popular is of course the emperor penguins, these little guys are often in droves among the various Iceland. Mirko's travel agency suffered a major blow, so that the sightseeing cruise was not worth the cost. The travel agency will build a bridge between Iceland and use a sightseeing bus to carry tourists. Mirko hopes to develop a computer program to manage the construction of these bridges in order to avoid unforeseen errors. These Iceland are labeled from 1 to N. At first these islands were not connected by a bridge, and the number of emperor penguins on all the islands was known. The number of penguins on each island varies, but it is always between [0, 1000]. Your program needs to handle the following three types of commands: 1. " Bridge a B "-establishes a bridge between A and B (A and B are different islands). Due to funding constraints, this order is accepted when and only if A and B are not connected. If this command is accepted, your program needs to output "yes" and then build the bridge. Otherwise, your program needs to output "no". 2. "Penguins A X" – according to reliable information, the number of emperor penguins at this time of island A becomes X. This command is used only to provide information, and your program does not need to respond. 3. "Excursion a B"-a tour group wishing to depart from A to B. If A and B are connected, your program needs to output the number of Emperor penguins (including start A and end B) you can see along the tour, and if not, your program needs to output "impossible". Input
The first line is a positive integer n, which represents the number of Iceland.
The second row n ranges [0, 1000] of integers, the number of the original emperor penguins for each island.
The third line is a positive integer m, which indicates the number of commands.
The next M-line is the command, as shown in the topic description.
Output
For each Bridge command with the Excursion command, output one line, as shown in the title description.
Sample INPUT5
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 5Sample Output4
Impossible
Yes
6
Yes
Yes
15
Yes
15
16HINT
Exercises
See this only the side of the edge has not been deleted always want to write a heuristic merger ... But since it is practice LCT, write LCT.
Code:
1#include <cstdio>2#include <cstdlib>3#include <cmath>4#include <cstring>5#include <algorithm>6#include <iostream>7#include <vector>8#include <map>9#include <Set>Ten#include <queue> One#include <string> A #defineINF 1000000000 - #defineMAXN 100000+5 - #defineMAXM 500+100 the #defineEPS 1e-10 - #definell Long Long - #definePA pair<int,int> - #defineFor0 (i,n) for (int i=0;i<= (n); i++) + #defineFor1 (i,n) for (int i=1;i<= (n); i++) - #defineFor2 (i,x,y) for (int i= (x); i<= (y); i++) + #defineFor3 (i,x,y) for (int i= (x); i>= (y); i--) A #defineMoD 1000000007 at using namespacestd; -InlineintRead () - { - intx=0, f=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} - while(ch>='0'&&ch<='9') {x=Ten*x+ch-'0'; ch=GetChar ();} in returnx*F; - } to intn,m,v[maxn],sum[maxn],c[maxn][2],fa[maxn],f[maxn],sta[maxn],top; + BOOLREV[MAXN]; -InlineintFindintx) {returnf[x]==x?x:f[x]=find (F[x]);} theInlineBOOLIsRootintx) * { $ returnc[fa[x]][0]!=x&&c[fa[x]][1]!=x;Panax Notoginseng } -InlinevoidPushup (intx) the { +sum[x]=sum[c[x][0]]+sum[c[x][1]]+V[x]; A } theInlinevoidRever (intx) + { -rev[x]^=1; Swap (c[x][0],c[x][1]); $ } $InlinevoidPushdown (intx) - { - if(!rev[x])return; theRever (c[x][0]); Rever (c[x][1]); -rev[x]=0;Wuyi } theInlinevoidRotateintx) - { Wu inty=fa[x],z=fa[y],l=c[y][1]==x,r=l^1; - if(!isroot (y)) c[z][c[z][1]==y]=x; Aboutfa[x]=z;fa[y]=x;fa[c[x][r]]=y; $c[y][l]=c[x][r];c[x][r]=y; - pushup (y);p ushup (x); - } -InlinevoidSplay (intx) A { +sta[++top]=x; the for(inty=x;! IsRoot (y); Y=fa[y]) sta[++top]=Fa[y]; - for(; top;) Pushdown (sta[top--]); $ while(!isroot (x)) the { the inty=fa[x],z=Fa[y]; the if(!isroot (y)) the { - if(c[z][0]==y^c[y][0]==x) rotate (x);Elserotate (y); in } the rotate (x); the } About } theInlinevoidAccessintx) the { the for(inty=0; x;x=Fa[x]) + { -Splay (x); c[x][1]=y;pushup (x); y=x; the }Bayi } theInlinevoidMakeroot (intx) the { - access (x); splay (x); Rever (x) ; - } theInlinevoidLinkintXinty) the { the if(Find (x) ==find (y)) {printf ("no\n");return;} theprintf"yes\n"); -Makeroot (x); Fa[x]=y;f[find (x)]=find (y); splay (x); the } theInlinevoidSplitintXinty) the {94 makeroot (x); Access (y); splay (y) ; the } the intMain () the {98Freopen ("Input.txt","R", stdin); AboutFreopen ("output.txt","W", stdout); -n=read ();101For1 (I,n) V[i]=sum[i]=read (), f[i]=i;102m=read ();103 while(m--)104 { the Charch[Ten];106scanf"%s", ch);intX=read (), y=read ();107 if(ch[0]=='b') Link (x, y);108 Else if(ch[0]=='P') splay (x), v[x]=y,pushup (x);109 Else if(Find (x)!=find (y)) printf ("impossible\n"); the ElseSplit (x, y), printf ("%d\n", Sum[y]);111 } the return 0;113}
View Code
BZOJ2843: Polar Travel Agency