[問題] Distance In Tree

看板Prob_Solve (計算數學 Problem Solving)作者 (~"~)時間12年前 (2012/03/12 01:24), 編輯推噓4(409)
留言13則, 7人參與, 最新討論串1/1
you are given a tree with n vertices and a positive number k. Find the number of distinct pairs of the vertices which have a distance of exactly k between them. Note that pairs (v, u) and (u, v) are considered to be the same pair. 1<= n <= 50000 1<= k <=500 沒想法@@ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.81.18.160

03/12 01:49, , 1F
用空間換時間的話,應該可在 O(n log n) 內解出來
03/12 01:49, 1F

03/12 01:50, , 2F
最慘也不過 O(n^2) ,暴力法硬上吧 :D
03/12 01:50, 2F

03/12 08:25, , 3F
codeforces...?
03/12 08:25, 3F

03/12 08:50, , 4F
03/12 08:50, 4F

03/12 08:50, , 5F
Codeforces的比賽幾乎都會附參考解答
03/12 08:50, 5F

03/12 09:16, , 6F
是 161D 這題嗎? 網頁上說 solution 是 O(n k)?
03/12 09:16, 6F

03/12 09:16, , 7F
不是應該 O(n) 就能得到解了? 還是我理解有誤?
03/12 09:16, 7F

03/12 09:43, , 8F
感謝 D 大的說明, 我一開始的想法有少考慮的 scenario
03/12 09:43, 8F

03/12 09:43, , 9F
為了避免誤導, 我把我原本錯誤的解答刪掉了
03/12 09:43, 9F

03/12 15:14, , 10F
看不懂-.-
03/12 15:14, 10F

03/12 19:28, , 11F
樓上不如說說看是哪裡看不懂
03/12 19:28, 11F

03/13 11:14, , 12F
O(nk) by tree DP 也可以 O(n lg n) by 樹分治
03/13 11:14, 12F

03/13 11:14, , 13F
前者實作上容易得多
03/13 11:14, 13F
文章代碼(AID): #1FND_4zB (Prob_Solve)
文章代碼(AID): #1FND_4zB (Prob_Solve)