Bzoj 4326 tree chain split + dichotomy + differential + memory

Source: Internet
Author: User

I didn't have a tree chain Noip last year!

Or a group of UOJ data cards.

The idea of difference is still very divine!

1#include <iostream>2#include <cstring>3#include <cstdio>4#include <algorithm>5#include <ctime>6#include <cstdlib>7 using namespacestd;8 Const intmaxn=300100;9 Const intinf=0x3f3f3f3f;Ten intN,m,u,v,w,cnt,tot,maxlen,minlen,minw,maxw,ans; One intHEAD[MAXN],DEP[MAXN],SIZ[MAXN],FATHER[MAXN],W[MAXN],P[MAXN],Q[MAXN],DIS[MAXN],LEN[MAXN],C[MAXN],TOP[MAXN], Memory[maxn],root; A structedge{intTo,next,w;} edge[maxn<<1]; -InlinevoidGet_int (int&x) - { thex=0;CharCh=getchar ();intf=1; -      while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();} -      while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=getchar ();} x*=F; - } +InlinevoidPut_int (intx) - { +     Charch[ -];inttop=0; A     if(x==0) ch[++top]='0'; at      while(x) ch[++top]=x%Ten+'0', x/=Ten; -      while(top) Putchar (ch[top--]); Putchar ('\ n'); - } -InlineintMax (intXintY) {returnX>y?x:y;} -InlineintMin (intXintY) {returnX>y?y:x;} -InlinevoidADD (intUintVintW) in{edge[cnt].to=v;edge[cnt].next=head[u];edge[cnt].w=w;head[u]=cnt++;} - //============================================== to voidDFS1 (intUintFA) + { -siz[u]=1; the      for(inti=head[u];i!=-1; i=edge[i].next) *         if(edge[i].to!=FA) $         {Panax Notoginsengdep[edge[i].to]=dep[u]+1; -father[edge[i].to]=u; thedis[edge[i].to]=dis[u]+EDGE[I].W; +w[edge[i].to]=EDGE[I].W; A DFS1 (edge[i].to,u); thesiz[u]+=siz[edge[i].to]; +         } - } $ voidDFS2 (intUintChainintFA) $ { -Top[u]=chain;intk=0; -      for(inti=head[u];i!=-1; i=edge[i].next) the         if(Edge[i].to!=fa && (siz[edge[i].to]>siz[k] | | k==0)) k=edge[i].to; -     if(k==0)return;Wuyi DFS2 (k,chain,u); the      for(inti=head[u];i!=-1; i=edge[i].next) -         if(Edge[i].to!=fa && k!=edge[i].to) DFS2 (edge[i].to,edge[i].to,u); Wu } -InlineintLCA (intUintv) About { $      while(true) -     { -         if(Top[u]==top[v])returnDEP[U]&GT;DEP[V]?v:u; -         if(Dep[top[u]]>dep[top[v]]) u=father[top[u];Elsev=Father[top[v]]; A     } + } the intGet (intUintf) - { $      for(inti=head[u];i!=-1; i=edge[i].next) the         if(f!=edge[i].to) thec[u]+=Get (edge[i].to,u); the     if(C[u]==tot) minlen=Max (Minlen,w[u]); the     returnC[u]; - } inInlineBOOLCheck (intt) the { themaxlen=minlen=tot=0; AboutMemset (c,0,sizeof(c)); the      for(intI=1; i<=m;i++) the         if(len[i]>t) the         { +c[p[i]]++,c[q[i]]++; -C[lca (P[i],q[i])]-=2; themaxlen=Max (Maxlen,len[i]);Bayitot++; the         } the     if(Memory[tot]) -     { -         if(memory[tot]<=t)return true; the         return false; the     } the Get (root,root); theMemory[tot]= (maxlen-Minlen); -     if(memory[tot]<=t)return true; the     return false; the } the intMain ()94 { theSrand (Time (0)); the get_int (n), Get_int (m); thememset (head,-1,sizeof(head)); minw=Inf;98      for(intI=1; i<n;i++) About     { - get_int (U), Get_int (v), Get_int (w);101 Add (u,v,w), add (v,u,w);102minw=Min (minw,w);103     }104      for(intI=1; i<=m;i++) Get_int (P[i]), Get_int (Q[i]); theRoot=rand ()%n+1;106Father[root]=root; dep[root]=1; dis[root]=0;107 DFS1 (root,root);108 DFS2 (root,root,root);109memset (Memory,0,sizeof(Memory)); the      for(intI=1; i<=m;i++) len[i]=dis[p[i]]+dis[q[i]]-2*dis[lca (P[i],q[i])],maxw=Max (Maxw,len[i]);111     intL=minw,r=Maxw; the      while(l<=R)113     { the         intMid= (l+r) >>1; the         if(Check (mid)) ans=mid,r=mid-1;ElseL=mid+1; the         117     }118 Put_int (Ans);119     return 0; -}
C + +

Bzoj 4326 tree chain split + dichotomy + differential + memory

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.