Блог пользователя autumncloud8

Автор autumncloud8, история, 7 лет назад, По-английски

Problem: For a tree with n vertices. Every vertex has a value in range [1, c]. We define d(u, v) is number of distinct vertices's value on simple path from u to v, and . We have to calculate all sum(i), 1 ≤ i ≤ n.

Constraints: 1 ≤ n ≤ 100000, 1 ≤ c ≤ 300000.

Do you have any idea?

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I have just came up with brute-force solution. I think we need math or some advance data structures

»
7 лет назад, # |
  Проголосовать: нравится -13 Проголосовать: не нравится

That problem is similar to this one: http://www.spoj.com/problems/COT2/

»
7 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can you post the link to the problem

»
7 лет назад, # |
  Проголосовать: нравится +16 Проголосовать: не нравится

I think we can solve this by centroid decomposition.

Start decomposing the tree. Say we have node R as our current centroid. Let T be the current tree. Let us now consider all paths in T that pass through R. Thus, after completely decomposing, all paths will be considered as they will pass thru some centroid R during the decomposition.

Do a dfs with R as root. For a node U in the ith subtree of R, let f(U) be the number of distinct colors from R to U . We now update sum(u) for paths that have the other endpoint in the first (i - 1) subtrees of R. Say we have cursum that stores the sum of no. of distinct colors for all paths from R to some node in the first (i - 1) subtrees of R. Also for each color C, we store curCount(C) as the no. of paths from R to some node in the first (i - 1) subtrees of R that contain the color C.

Update sum(u) +  = (f(U) * #nodes - in - first - (i - 1) - subtrees) + cursum - sigma(curcount(C)). The summation is over the colors C which appear in the path from R to U. All the above calculation can be done in O(|T|). Hence overall complexity is O(NlogN)

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Is there any solution for this problem without using centroid?

    Thanks in advance