EBAD's blog

By EBAD, history, 3 months ago, In English,

Given a weighted graph with n nodes and m edges. for each node v we should calculate number of nodes u such that d(v,u)<=k . n,m<=5e4 k<=100; Please somebody help me. UPD: I don't know if it's important or not but Wi<=100 for every edge.

 
 
 
 

»
3 months ago, # |
  Vote: I like it -18 Vote: I do not like it

As k is small here. You can do DP with states — DP(u, k) =  number of nodes in subtree of u at distance k. Then you need to combine answers for subtrees. Complexity O(nk). (Notice that we are not interested in any path of length  > k)

However this problem can also be solved in O(nlogn), for large k, using Centroid Decomposition.
You can submit the unweighted graph version of this problem here — 161D - Distance in Tree.

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by EBAD (previous revision, new revision, compare).

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Are negative weights allowed?