[Training question 1025 -- selecting Wood] tree grouping and monotonous queue

Source: Internet
Author: User

Question:

Calculate the length of the previous tree as L ~ The path with the largest weight between R. The length is the number of edges in this path. n <= 100000, L <= r <= N

Analysis:

Direct Tree-based DP is difficult. Because it is used to find the XX path in a tree, it is easy to think of tree partitioning (see qzc)

Algorithm

Here we will talk about a point-based splitting. the center of gravity of the decision tree is the root. First, we will calculate and update the answer through our own path, and then Recursion to the subtree.

Process each subtree sequentially. First, the length of the subtree is 1 ~ All paths of R are stored in an array with the maximum weight of each depth of the path. The current subtree is used to "combine" the array in the above sentence, after "merging", update the "array" with the information area of the current subtree. The "merging" process is equivalent to one DP, that is, max {A [I] + B [J] And l <= I + j <= r} are obtained in two linear tables, which are implemented using monotonic queues.

For the sake of fun, I made up a line segment number to "combine". For this reason, shield lost 10 chicken nuggets. Haha, the line segment trees have passed 0.2 s (monotonous queue S)

Code (line segment tree ):

{$ M 10000000 }{$ inline on} <br/> program syj; <br/> uses math; <br/> const oo = maxlongint> 1; <br/> var I, X, Y, Z, Ed, N, LC, RC, E, TOT, Minx, mi, ANS, T: longint; <br/> H, qd, QS, son, Si: array [1 .. 100005] of longint; <br/> next, point, W, L, R, B, BB, MA: array [1 .. 300005] of longint; <br/> V: array [1 .. 100005] of Boolean; <br/> procedure dfs1 (I, fa, DEP, S: longint); inline; <br/> var J, K: longint; <br/> begin <br/> If Dep <= RC then begin <br/> Inc (ed); QD [ed]: = Dep; QS [ed]: = s; <br/> end; <br/> J: = H [I]; son [I]: = 1; <br/> while j <> 0 do begin <br/> K: = point [J]; <br/> If (k <> FA) and not V [k] Then begin <br/> dfs1 (K, I, DEP + 1, S + W [J]); <br/> Inc (son [I], son [k]); <br/> end; <br/> J: = next [J]; <br/> end; <br/> procedure dfs2 (I, FA: longint); inline; <br/> var J, K, Mins: longint; <br/> begin <br/> mins: = tot-son [I]; J: = H [I]; <br/> while j <> 0 do begin <br/> K: = point [J]; <br/> If (k <> FA) and not V [k] Then begin <br/> dfs2 (K, I); <br/> If Son [k]> mins then mins: = Son [k]; <br/> end; <br/> J: = next [J]; <br/> end; <br/> If mins <Minx then begin <br/> Minx: = mins; MI: = I; <br/> end; <br/> procedure maxi (VAR X: longint; Y: longint); inline; <br/> begin <br/> if y> X then X: = y; <br/> end; <br/> procedure Update (I, ll, RR, K: longint); inline; <br/> var mid: longint; <br/> begin <br/> If ll> RR then exit; <br/> If (L [I] = LL) and (R [I] = RR) then begin <br/> B [I]: = K; maxi (MA [I], k); exit; <br/> end; <br/> mid: = (L [I] + R [I]) Div 2; <br/> if BB [I]>-oo-1 then begin <br/> BB [I * 2]: = BB [I]; bb [I * 2 + 1]: = BB [I]; MA [I * 2]: = BB [I]; MA [I * 2 + 1]: = BB [I]; <br/> B [I]: =-oo-1; bb [I]: =-oo-1; <br/> end; <br/> If B [I]>-oo-1 then begin <br/> B [I * 2]: = B [I]; B [I * 2 + 1]: = B [I]; maxi (MA [I * 2], B [I]); maxi (MA [I * 2 + 1], B [I]); <br/> B [I]: =-oo-1; <br/> end; <br/> Update (I * 2, ll, min (MID, RR ), k); Update (I * 2 + 1, max (Mid + 1, LL), RR, k); <br/> ma [I]: = max (MA [I * 2], ma [I * 2 + 1]); <br/> end; <br/> function ask (I, ll, RR: longint): longint; inline; <br/> var mid: longint; <br/> begin <br/> If ll> RR then exit (-oo ); <br/> If (L [I] = LL) and (R [I] = RR) Then exit (MA [I]); <br/> if BB [I]>-oo-1 then begin <br/> BB [I * 2]: = BB [I]; bb [I * 2 + 1]: = BB [I]; MA [I * 2]: = BB [I]; MA [I * 2 + 1]: = BB [I]; <br/> B [I]: =-oo-1; bb [I]: =-oo-1; <br/> end; <br/> If B [I]>-oo-1 then begin <br/> B [I * 2]: = B [I]; B [I * 2 + 1]: = B [I]; maxi (MA [I * 2], B [I]); maxi (MA [I * 2 + 1], B [I]); <br/> B [I]: =-oo-1; <br/> end; <br/> mid: = (L [I] + R [I]) Div 2; <br/> ask: = max (ASK (I * 2, ll, min (MID, RR), ask (I * 2 + 1, max (Mid + 1, LL), RR); <br/> end; <br/> procedure work (I: longint); inline; <br/> var J, K, LL: longint; <br/> begin <br/> V [I]: = true; J: = H [I]; MA [1]: =-Oo; bb [1]: =-Oo; B [1]: =-oo-1; Update (,); <br/> while j <> 0 do begin <br/> K: = point [J]; <br/> if not V [k] Then begin <br/> ed: = 0; <br/> dfs1 (K, 0, 1, W [J]); <br/> for LL: = 1 to Ed do maxi (ANS, ask (1, max (0, LC-Qd [ll]), RC-Qd [ll]) + qs [ll]); <br/> for LL: = 1 to Ed do update (1, qd [ll], qd [ll], QS [ll]); <br/> end; <br/> J: = next [J]; <br/> end; <br/> J: = H [I]; <br/> while j <> 0 do begin <br/> K: = point [J]; <br/> if not V [k] Then begin <br/> Minx: = n + 1; Tot: = Son [k]; <br/> dfs2 (K, 0); <br/> Work (MI); <br/> end; <br/> J: = next [J]; <br/> end; <br/> end; <br/> procedure build (I, ll, RR: longint); inline; <br/> var mid: longint; <br/> begin <br/> If ll> RR then exit; <br/> L [I]: = ll; R [I]: = RR; B [I]: =-oo-1; MA [I]: =-oo-1; Mid: = (LL + RR)> 1; <br/> If LL = RR then exit; <br/> build (I * 2, ll, mid); Build (I * 2 + 1, Mid + 1, rr); <br/> end; <br/> procedure Link (x, y, z: longint); <br/> begin <br/> Inc (E ); next [e]: = H [X]; point [e]: = y; W [e]: = z; H [x]: = E; <br/> end; <br/> begin <br/> assign (input, 'wood. in '); reset (input); <br/> assign (output, 'wood. out'); rewrite (output); <br/> readln (n, LC, RC); <br/> for I: = 1 to n-1 do begin <br/> readln (x, y, z); Link (x, y, z); Link (Y, x, z ); <br/> end; <br/> ans: =-Oo; <br/> build (1, 0, n); <br/> work (1 ); <br/> writeln (ANS); <br/> close (input); close (output); <br/> end. <br/>

Key code of monotonous queue:

Procedure deal (I: longint); var J, K, i1, I2, T: longint; <br/> begin <br/> K: = d [I]; J: = P [k]; O [I]: = true; T: = 0; <br/> while K <> 0 do begin <br/> if not o [J] Then begin <br/> MAX: = 0; dfs1 (I, j, c [K], 1); <br/> beg: = 1; clo: = 0; I2: =-1; <br/> for I1: = max downto 0 do begin <br/> for I2: = I2 + 1 to r-i1 DO <br/> If I2 <= t then begin <br/> while (beg <= Clo) and (A [Q [clo] <= A [I2]) Do Dec (ClO); <br/> Inc (ClO); Q [clo]: = I2; <br/> end else break; <br/> while (beg <= Clo) and (Q [beg] + I1 <L) Do Inc (BEG ); <br/> If (beg <= Clo) and (A [Q [beg] + B [I1]> ans) <br/> then ans: = A [Q [beg] + B [I1]; <br/> end; <br/> If max> T then for T: = t + 1 to Max do a [T]: =-(1 <29); <br/> for I1: = 1 to Max do <br/> If B [I1]> A [I1] Then a [I1]: = B [I1]; <br/> tot: = s [J]; min: = maxlongint; <br/> dfs2 (I, j); go [J]: = best; <br/> end; <br/> K: = next [k]; J: = P [k]; <br/> end; <br/> K: = d [I]; J: = P [k]; <br/> while K <> 0 do begin <br/> if not o [J] and (s [J]> = L) then deal (go [J]); <br/> K: = next [k]; J: = P [k]; <br/> end; <br/> end;

Unfortunately, this monotonous queue collapsed during the exam, and the question of SHIELD's brother was disgusting.

Ms is a simplified version of WC.

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.