[Template] [P3806] points divide and conquer

(7.17) has long wanted to learn the partition of the state today ...... is not online, a blind will write a note to reasonable grounds ideas.

--------------------------------------------------------------------

  (Static) point divide and conquer is an unrooted trees using the idea of ​​partition of the nature of violence, the statistics may be weighted tree path information in the complexity of O (nlog ^ 2n) of.

  Examples like this channel, asking whether there is a plurality of sets of length k of the path, we need to pre-store a path length information of all buckets.

  Point divide and conquer approach is to select a suitable root node, all paths tree is not heavy into two parts does not leak to the statistics:

  1, the root node u through the path;

  2, u path in a sub-tree.

  Every time we partition the statistics information of a first path, then recursively into each sub-tree u, the second path will be seen as sub-issues within its sub-tree to solve.

  First of all, we need to select a suitable root node partition. Ideally root to meet each of its sub-tree size are basically the same size; so we think the focus of this good stuff.

   Unrooted trees gravity u properties:

    1, the minimum size of the largest sub-tree.

    2, the maximum size of less than half the sub-tree is equal to the size of the tree.

  If the center of gravity of each selected sub-tree to the root partition, we can ensure that no more than logn recursive layer.

  1. void find_rt(int u, int pre) {  
  2.     size[u] = 1;  
  3.     int Mx = 0;  
  4.     for (int i = head[u]; i; i = edge[i].nxt) {  
  5.         int v = edge[i].to;  
  6.         if (v == pre || vis[v]) continue;  
  7.         find_rt(v, u);  
  8.         size[u] += size[v];  
  9.         Mx = max(Mx, size[v]);  
  10.     }  
  11.     Mx = max(Mx, Size - size[u]);  
  12.     if (Mx < Mn)   
  13.         root = u, Mn = Mx;  

--------------------------------------------------------------------

  Next is the process of partition.

  For this question, we statistics by a violent dfs the path information for each point of the current sub-tree (including the root itself, the depth is 0), then continue to combinations of two very violent path, and then on the problem ... ...

  Any two paths merge it is not feasible, because the two paths may be from the same child node v u. At this point we got this information is not legitimate path to u ---> v statistics of this edge twice, so we again iterate over each of its sub-tree, remove these illegal paths. Specific operations can look at the code, we use the principle of inclusion and exclusion.

 

  1. void dfs(int u, int pre, int depth) {  
  2.     chd [++ tot] = depth; // record the depth of each child node
  3.     for (int i = head[u]; i; i = edge[i].nxt) {  
  4.         int v = edge[i].to;  
  5.         if (v == || the force [in])   
  6.             continue;  
  7.         dfs(v, u, depth + edge[i].w);  
  8.     }  
  9. }  
  10. Solve void ( int U,  int Extra,  BOOL F) {// third parameter represents a subtraction
  11.     tot = 0;  
  12.     dfs(u, 0, extra);  
  13.     if (f) {  
  14.         for (int i = 1; i <= tot; ++i)  
  15.             for (int j = i + 1; j <= tot; ++j)   
  16.                 ++ military [Safety [i] + Safety [j]];  
  17.     } else {  
  18.         for (int i = 1; i <= tot; ++i)  
  19.             for (int j = i + 1; j <= tot; ++j)   
  20.                 --ans [Safety [i] + Safety [j]];  
  21.     }  
  22. }  
  23. Divide void ( int U) {// Process the partition
  24.     vis[u] = true;  
  25.     solve(u, 0, 1); 
  26.     for (int i = head[u]; i; i = edge[i].nxt) {  
  27.         int v = edge[i].to;  
  28.         if (vis[v]) continue;  
  29.         solve (v, edge [i] .w, 0); // The second parameter is the initial depth, and to ensure uniform depth u root calculated.
  30.         Mn = inf, Size = size[v];  
  31.         find_rt(v, 0);  
  32.         divide(root);  
  33.     }  
  34. }  

  No matter who probably want to see here Tucao: this constant big gone? Every time again have to run more than O (n) search and O (n ^ 2) statistics, although the complexity has not changed, but it can not be accepted.

  In fact the process of divide and conquer has written a second, but I have yet to master, so to be here today on the first update.

Guess you like

Origin www.cnblogs.com/TY02/p/11203163.html