[bzoj3611] [HEOI2014] Big project

Source: Internet
Author: User

Look at the topic feeling should be the legend of the virtual tree?

and ran to learn a ... I yy a bit and then hang fly. So I had to copy the template t_t

Build the virtual tree is a tree-shaped DP ...

For the sum of queries: the contribution of each edge to the answer is the number of nodes on the edge * One end of the node * at the other end. (The node here does not include the point added when the virtual tree is built)

For the maximum value of the query minimum, each time you calculate the longest pass through this node | | The shortest path length is good.

Probably the condition has a sigma (K) <=n and the like. And the question asks the thing to conform to the interval addition ... Otherwise, it won't work if you put the side together. >_<

The chain profile for LCA is fast ... Speed to the top 10 ... However the code length is really touching = =

1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5 #definell Long Long6 using namespacestd;7 Const intmaxn=1000023;8 Const intinf=1002333333;9 structzs{Ten     intToo,pre; One}e[maxn<<1]; A structzs1{ -     intToo,pre,dis; -}e1[maxn<<2]; the intTOT,TOT1,LAST[MAXN],LAST1[MAXN]; - intSZ[MAXN],MN[MAXN],MX[MAXN]; - intIntree[maxn],poi[maxn],rt; - intDfn[maxn],bel[maxn],size[maxn],dep[maxn],fa[maxn],tim; + intSt[maxn],top; - intAnsmn,ansmx; + ll Anssum; A intI,j,k,n,m,k,a,b,lca; at   -   - intRaCharRx; -InlineintRead () { -Rx=getchar (), ra=0; -      while(rx<'0'|| Rx>'9') rx=GetChar (); in      while(rx>='0'&&rx<='9') ra*=Ten, ra+=rx- -, Rx=getchar ();returnRA; - } to   +   -InlinevoidInsertintAintb) { thee[++tot].too=b,e[tot].pre=last[a],last[a]=tot; *e[++tot].too=a,e[tot].pre=last[b],last[b]=tot; $ }Panax NotoginsengInlinevoidInsintAintb) { - //printf ("%d-->%d\n", A, b); thee1[++tot1].too=b,e1[tot1].dis=dep[b]-dep[a],e1[tot1].pre=last1[a],last1[a]=tot1; + } A   the   + voidDFS1 (intx) { -size[x]=1; $      for(intI=last[x];i;i=e[i].pre) $         if(e[i].too!=Fa[x]) -fa[e[i].too]=x, - DFS1 (E[i].too), thesize[x]+=Size[e[i].too]; - }Wuyi voidDFS2 (intXintchain) { thebel[x]=chain,dfn[x]=++tim,dep[x]=dep[fa[x]]+1;intmxpos=0, I,to; -      for(to=e[i=last[x]].too;i;to=e[i=E[i].pre].too) Wu         if(To!=fa[x]&&size[to]>size[mxpos]) mxpos=to ; -     if(!mxpos)return; About DFS2 (mxpos,chain); $      for(to=e[i=last[x]].too;i;to=e[i=E[i].pre].too) -         if(to!=fa[x]&&to!=mxpos) DFS2 (to,to); - } -InlineintGetlca (intAintb) { A     if(dep[bel[a]]<dep[bel[b]) swap (A, b); +      while(bel[a]!=Bel[b]) { theA=Fa[bel[a]]; -         if(dep[bel[a]]<dep[bel[b]) swap (A, b); $     } the     returnDEP[A]&LT;DEP[B]?a:b; the } the   the   - BOOLcmpintAintb) {returndfn[a]<dfn[b];} in   theInlineintMinintAintb) {returnA<b?a:b;} theInlineintMaxintAintb) {returnA>b?a:b;} About voiddpintx) { theRegisterinti,to; the     if(intree[x]==m) sz[x]=1, mn[x]=mx[x]=0; the         Elsesz[x]=0, mn[x]=inf,mx[x]=-inf; +      for(to=e1[i=last1[x]].too;i;to=e1[i=E1[i].pre].too) { -DP (to), sz[x]+=sz[to],mn[to]+=e1[i].dis,mx[to]+=E1[i].dis; theanssum+= (LL) sz[to]* (K-sz[to]) *E1[i].dis;Bayi         if(MN[X]+MN[TO]&LT;ANSMN) ansmn=mn[x]+Mn[to]; the         if(mx[to]+mx[x]>ansmx) ansmx=mx[x]+Mx[to]; the         if(Mn[to]<mn[x]) mn[x]=mn[to];if(Mx[to]>mx[x]) mx[x]=Mx[to]; -     } -last1[x]=0; the } the   the   the Chars[ at];intLen; -Inlinevoidoutll (ll x) { the     if(!x) {Putchar ('0');return;} the      for(len=0; x;s[++len]=x%Ten, x/=Ten); the      while(len) Putchar (s[len--]+ -);94 } theInlinevoidOutint (intx) { the     if(!x) {Putchar ('0');return;} the      for(len=0; x;s[++len]=x%Ten, x/=Ten);98      while(len) Putchar (s[len--]+ -); About } -  101  102 intMain () {103Registerinti;104n=read (); the      for(i=1; i<n;i++) A=read (), b=read (), insert (A, b);106DFS1 (1), DFS2 (1,1);107 //for (i=1;i<=n;i++) printf ("%d%d\n", Dep[i],dfn[i]);108 //for (i=1;i<n;i++) for (j=i+1;j<=n;j++) printf ("%d&&%d%d\n", I,j,getlca (I,j));109      for(M=read (); m;m--){ thetot1=0;111          for(K=read (), i=1; i<=k;i++) Intree[poi[i]=read ()]=m; theSort (poi+1, poi+1+K,CMP); top=1, st[1]=poi[1];113 //for (i=1;i<=k;i++) printf ("%d\n", Poi[i]); the          for(i=2; i<=k;i++){ theLca=Getlca (Poi[i],st[top]); the              while(dfn[lca]<dfn[st[top]]&&top)117                 if(dfn[st[top-1]]<=Dfn[lca]) {118INS (lca,st[top--]);119                     if(ST[TOP]!=LCA) st[++top]=LCA; -}ElseINS (st[top-1],st[top]), top--;121st[++top]=Poi[i];122         }123          while(top>1) Ins (st[top-1],st[top]), top--;124ansmn=inf,ansmx=anssum=0, rt=st[1], the DP (RT);126Outll (Anssum), Putchar (' '), Outint (ANSMN), Putchar (' '), Outint (Ansmx), Putchar ('\ n');127     } -     return 0;129}
View Code

[bzoj3611] [HEOI2014] Big project

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.