Tree chain Tree chain Split Split Comments (Los Valley template P3384)

 

Preface:

  First, before learning the best first split tree chain LCA, tree-DP, DFS sequence of these three knowledge learned
  emm still necessary before the chain to star, tree line also first learned.

  If these three points did not grasp the knowledge is good, tree-chain split is of course difficult to understand.

I. Introduction

  Tree chain is split into several chains of a tree, the tree becomes linear, the difficulty of reducing the processing

  Need issues addressed:

    •  All tree nodes from x to y shortest path node values ​​plus z
    •  Tree request from x to y values ​​of all nodes in the shortest path of nodes and
    •  X is a root node will be the subtree nodes values ​​are all plus z
    •  Find all the nodes in the value x is a root node of the subtree and

  concept

    • Heavy sons: For each non-leaf node, its son to the son of the eldest son, is the root sub-tree nodes for re-son node (Ps: thank @shzr chiefs pointed out this sentence does not express my rigorous qwq, modified)
    • Light sons: For each non-leaf node, it is the son of the son of the remaining heavy all-Africa is the son of the son of light
    • Leaf node does not light nor heavy son son (because it has no son ..)
    • Heavy side: a connection to his father's son heavy edges are called multiple edges // original wording: re-connect any two sons called the heavy side edge
    • Light side: the rest is the light side
    • Heavy chain: heavy side adjacent to link connecting a heavy chain called the son of the heavy chain
      • For the leaf node, if it is the son of light, there is a starting point to themselves as the length of the chain 1
      • Each heavy chain to light his son as a starting point

  nature

    If the edge (u, v) as a light side, then, size (u)> = size (v) * 2

    The number of the root node through a path of light more than a certain side of log (n)

    The number of the root node through a path of the heavy chain constant no more than log (n)

Second, the algorithm processes

  dfs1 ()

    The dfs1 to deal with a few things:

    • Deep mark each point depth []
    • Mark each point's father fa []
    • Each non-leaf nodes labeled subtree size (including its own)
    • Mark each non-leaf nodes of heavy son number w_son []
    • inline void DFS1 ( int u, int Father, int fdeep) 
      { 
          Deep [u] = fdeep, FA [u] = Father,, root_size [u] = . 1 ; // tag node's father, depth, u is the root to the current the number of nodes in the tree 
          int pw_son = - . 1 ; // presupposition son root_size weight of -1 
          for ( int I = head [U]; I; I = Edge [I] .next) 
          { 
              IF (Edge [ I] == .to Father) Continue ; // undirected graph "throwback side" do not need 
              DFS1 (edge [I] .to, U, fdeep + . 1 ); // search son nodes 
              if(root_size[edge[i].to]>pw_son) w_son[u]=edge[i].to;    
              root_size[u]+=root_size[edge[i].to];//更新重儿子 
          }
      }

  dfs2()

    This pretreatment also dfs2 few things

    • No new id tag for each point
    • The initial value assigned to each point on the new number
    • Processing each point where the top strand
    • Each processing chain
    • inline void DFS2 ( int u, int FTOP) 
      { 
          Top [u] = FTOP; // u is added to the first strand of the chain FTOP 
          ID [u] = CNT ++; // record the timestamp of each node 
          W [ CNT] = A [U]; // according to the timestamp value added weights 
          IF (son [U]!) return ; // no son, direct return 
          DFS2 (son [U], FTOP); // first deep search heavy son , this will ensure that the sequence of the heavy son as small as possible 
          for ( int I = haed [U]; I; I = Edge [I] .next)
               IF ! (Edge [I] = U && .to Edge [I] .to ! = Son [U]) 
                  DFS2 (edge [i] .to, edge [i] .to) // generated in an edge [i] .to a new chain link head
      }

  FIG effect as follows (the lazy little) shown in FIG.

  

  For example the following issues addressed in the template

    • Processing spot on the right path between any two points, and
    • 处理一点及其子树的点权和
    • 修改任意两点间路径上的点权
    • 修改一点及其子树的点权

  修改任意两点间路径上的点权

    设所在链顶端的深度更深的那个点为x点

    • ans加上x点到x所在链顶端 这一段区间的点权和
    • 把x跳到x所在链顶端的那个点的上面一个点

    不停执行这两个步骤,直到两个点处于一条链上,这时再加上此时两个点的区间和即可

 

  处理任意两点间路径上的点权和

  修改一点及其子树的点权

  处理一点及其子树的点权和

 

 

三、例题

  树链剖分模板

四、相关转载于推荐文章(十分感谢这些博主)

  树链剖分详解(洛谷模板 P3384)

 

 

Guess you like

Origin www.cnblogs.com/SeanOcean/p/11294155.html