[Tree governance] poj1741

Source: Internet
Author: User

A tree is a beautiful data structure (well, I have read this sentence hundreds of times ).

 

The description of this question is very clear. I will give you a rootless tree with edge weights for N points (n <10000). You need to count the number of vertices whose distance is not greater than K;

 

Because multiple groups of data exist, the square algorithm cannot be used, so it approaches nlogn (which Nb data structure can be O (n )? Oh, my God ..).

After careful analysis, I actually read the paper. For each matching point, there is a path between them. For each point, either on this path or outside the path, this reminds us of the use of the sharding algorithm:

For each tree, we find the optimal root, so that the child tree with the root of his son is the largest, the largest, and the smallest. It makes sense to select a link to minimize the number of layers after recursion. It is easy to prove that the number of layers is smaller than or equal to the number of logn layers (a chain in the worst case ).

We have found this root, and we only need to count the number of entries passing through his path. The condition is Dep (I) + Dep (j) <= K (I and j belong to different sub-trees)-> Dep (I) <= k-dep (j). I use treap for statistics, it seems that the linear scanning of O (n) can be performed without any solution.

After you have done it yourself, you can go to every subtree recursively. Because there are a total of logn layers, each layer requires nlogn statistics, so the complexity is n log2n, the speed is very fast, 300 ms.

 

The code feels good:

 

Program poj1741; <br/> var <br/> D, son, l, R, V, W, next, P, C, S, go: array [0 .. 30000] of longint; <br/> O: array [0 .. 20000] of Boolean; <br/> N, Len, best, I, Min, T, TOT, TMP, ANS, root, a, B, CC: longint; </P> <p> procedure left (var I: longint); begin <br/> TMP: = R [I]; R [I]: = L [TMP]; L [TMP]: = I; <br/> S [I]: = s [L [I] + s [R [I] + 1; <br/> S [TMP]: = s [L [TMP] + s [R [TMP] + 1; <br/> I: = TMP; <br/> end; </P> <p> procedure right (var I: longint); begin <br/> TMP: = L [I]; L [I]: = R [TMP]; R [TMP]: = I; <br/> S [I]: = s [L [I] + s [R [I] + 1; <br/> S [TMP]: = s [L [TMP] + s [R [TMP] + 1; <br/> I: = TMP; <br/> end; </P> <p> procedure insert (var I: longint; K: longint); begin <br/> If I = 0 then begin <br/> Inc (t ); i: = T; <br/> S [I]: = 1; <br/> W [I]: = K; <br/> V [I]: = random (maxlongint); <br/> L [I]: = 0; R [I]: = 0; <br/> end else <br/> If W [I]> K then begin <br/> insert (L [I], k ); INC (s [I]); <br/> If V [L [I]> V [I] Then right (I ); <br/> end else begin <br/> insert (R [I], k); Inc (s [I]); <br/> If V [R [I]> V [I] Then left (I); <br/> end; </P> <p> function rank (I, K: longint): longint; begin <br/> If I = 0 Then exit (0 ); <br/> If W [I] <= k then rank: = s [L [I] + 1 + rank (R [I], K) else <br/> rank: = rank (L [I], k); <br/> end; </P> <p> procedure Link (a, B, CC: longint); begin <br/> Inc (t); <br/> next [T]: = d [a]; d [A]: = T; P [T]: = B; C [T]: = cc; <br/> end; </P> <p> procedure dfs1 (FA, I, DEP: longint); <br/> var K, J: longint; <br/> begin <br/> Inc (ANS, rank (root, len-dep )); <br/> K: = d [I]; J: = P [k]; <br/> son [I]: = 1; <br/> while K <> 0 do begin <br/> If (j <> FA) and (not o [J]) then begin <br/> dfs1 (I, j, DEP + C [k]); <br/> Inc (son [I], son [J]); <br/> end; <br/> K: = next [k]; J: = P [k]; <br/> end; </P> <p> procedure dfs2 (FA, I, dep: longint); <br/> var K, J, Ms: longint; <br/> begin <br/> insert (root, DEP); <br/> MS: = tot-son [I]; <br/> K: = d [I]; J: = P [k]; <br/> while K <> 0 do begin <br/> If (j <> FA) and (not o [J]) then begin <br/> dfs2 (I, j, DEP + C [k]); <br/> If Son [J]> MS then MS: = Son [J]; <br/> end; <br/> K: = next [k]; J: = P [k]; <br/> end; <br/> If Ms <min then begin <br/> min: = MS; best: = I; <br/> end; </P> <p> procedure deal (I: longint); <br/> var K, J: longint; <br/> begin <br/> O [I]: = true; <br/> root: = 0; T: = 0; <br/> insert (root, 0); <br/> K: = d [I]; j: = P [k]; <br/> while K <> 0 do begin <br/> if not o [J] Then begin <br/> min: = maxlongint; <br/> dfs1 (I, j, C [k]); Tot: = Son [J]; <br/> dfs2 (I, j, C [k]); 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] Then deal (go [J]); <br/> K: = next [k]; J: = P [k]; <br/> end; </P> <p> begin <br/> readln (n, Len); <br/> randomize; <br/> while n <> 0 do begin <br/> T: = 0; ans: = 0; <br/> fillchar (O, sizeof (O ), false); <br/> fillchar (D, sizeof (D), 0); <br/> for I: = 1 to n-1 do begin <br/> readln (, b, CC); <br/> Link (a, B, CC); Link (B, A, CC); <br/> end; <br/> deal (1); <br/> writeln (ANS); <br/> readln (n, Len); <br/> end; <br/> end. <br/>

Related Article

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.