"BZOJ2843" Polar travel agency offline + Tree chain split + tree-like array

Source: Internet
Author: User

"BZOJ2843" Polar travel agency 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 first line 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. 1<=n<=30000,1<=m<=100000output

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
the -

Puzzle: Most people on the net are written with LCT, feel with LCT code should be more than the tree cut a lot ~ But the tree is still small constant

Read the entire graph offline, use and look up a set of even those sides, and then no brain tree profile.

#include <cstdio> #include <iostream> #include <cstring>using namespace std;const int maxn=30010; const int Maxm=100010;int N,m,cnt;int TO[MAXN&LT;&LT;1],NEXT[MAXN&LT;&LT;1],V[MAXN],F[MAXN],HEAD[MAXN],S[MAXN],DEP [Maxn],fa[maxn],top[maxn];int Op[maxm],qa[maxm],qb[maxm],siz[maxn],son[maxn],p[maxn];int Rd () {int Ret=0,f=1;char Gc=getchar (); while (gc< ' 0 ' | | Gc> ' 9 ') {if (gc== '-') F=-f;gc=getchar ();} while (gc>= ' 0 ' &&gc<= ' 9 ') ret=ret*10+gc-' 0 ', Gc=getchar (); return ret*f;} Char str[10];int find (int x) {return (f[x]==x) x: (F[x]=find (f[x]));} void Add (int a,int b) {to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;} void dfs1 (int x) {siz[x]=1;for (int i=head[x];i!=-1;i=next[i]) if (!dep[to[i]]) {DEP[TO[I]]=DEP[X]+1,FA[TO[I]]=X,DFS1 ( To[i]), Siz[x]+=siz[to[i]];if (Siz[to[i]]>siz[son[x]) son[x]=to[i];}} void dfs2 (int x,int tp) {top[x]=tp,p[x]=++p[0];if (son[x]) DFS2 (SON[X],TP); for (int i=head[x];i!=-1;i=next[i]) if (To[i] !=FA[X]&AMP;&AMP;TO[I]!=SON[X]) DFS2 (To[i],to[i]);} void Updata (int x,int val) {for (int i=x;i<=n;i+=i&-i) S[i]+=val;} int query (int x) {int i,ret=0;for (i=x;i;i-=i&-i) ret+=s[i];return ret;} int ask (int x,int y) {int ret=0;while (Top[x]!=top[y]) {if (Dep[top[x]]<dep[top[y]]) swap (x, y); Ret+=query (P[x])- Query (P[TOP[X]]-1), x=fa[top[x]];} if (Dep[x]>dep[y]) swap (x, y), Ret+=query (P[y])-query (p[x]-1); return ret;} int main () {n=rd (), int i;for (i=1;i<=n;i++) v[i]=rd (), F[i]=i;m=rd (), memset (Head,-1,sizeof (head)); for (i=1;i<=m ; i++) {scanf ("%s", str), Qa[i]=rd (), Qb[i]=rd (), if (str[0]== ' B ') {op[i]=1;if (Find (Qa[i]) ==find (Qb[i]) Op[i]=-1;elsef [F[qa[i]]]=f[qb[i]],add (Qa[i],qb[i]), add (Qb[i],qa[i]);} if (str[0]== ' P ') op[i]=2;if (str[0]== ' e ') {op[i]=3;if (Find (Qa[i])!=find (Qb[i])) op[i]=-3;}} for (i=1;i<=n;i++) if (!dep[i]) DEP[I]=1,DFS1 (i), DFS2 (I,i); for (i=1;i<=n;i++) Updata (P[i],v[i]); for (i=1;i<= m;i++) {if (op[i]==-1) printf ("no\n"), if (op[i]==-3) printf ("impossible\n"), if (op[i]==1) printf ("yes\n"); if (op[i]==2 ) Updata (P[qa[i]],qb[i]-v[qa[i]]), v[qa[i]]=qb[i];if (op[i]==3) printf ("%d\n", Ask (QA[I],QB[i]));} return 0;}

"BZOJ2843" Polar travel agency offline + Tree chain split + tree-like array

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.