Part II
Subordinates | Tree Matching |
Tree Diameter | Tree Distances I |
Tree Distances II | Company Queries I |
Company Queries II | Distance Queries |
Counting Paths | Subtree Queries |
Path Queries | Path Queries II |
Distinct Colors | Finding a Centroid |
Fixed-Length Paths I | Fixed-Length Paths II |
Prüfer Code | Tree Traversals |
Tree Isomorphism I | Tree Isomorphism II |
Subordinates | Tree Matching |
Tree Diameter | Tree Distances I |
Tree Distances II | Company Queries I |
Company Queries II | Distance Queries |
Counting Paths | Subtree Queries |
Path Queries | Path Queries II |
Distinct Colors | Finding a Centroid |
Fixed-Length Paths I | Fixed-Length Paths II |
Prüfer Code | Tree Traversals |
Tree Isomorphism I | Tree Isomorphism II |
CF 1843F2
CF 609E
CF 1706E
CSA Root LCA Queries
OI 暴雷
TOI 初選 '21 pD
JOI 本選 '23 pD
BOI '17 D1 pB
哇我題目真的刷很少
Subordinates | Tree Matching |
Tree Diameter | Tree Distances I |
Tree Distances II | Company Queries I |
Company Queries II | Distance Queries |
Counting Paths | Subtree Queries |
Path Queries | Path Queries II |
Distinct Colors | Finding a Centroid |
Fixed-Length Paths I | Fixed-Length Paths II |
Prüfer Code | Tree Traversals |
Tree Isomorphism I | Tree Isomorphism II |
Atcoder DP P
CF 1528A
CF 161D
CF 1521D
CF 1000G
OI 暴雷
BOI '20 D2 pB (Minimum)
TOI 初選 '20 pB
全國模擬賽 '22 pF
請不要講爛梗
Subordinates | Tree Matching |
Tree Diameter | Tree Distances I |
Tree Distances II | Company Queries I |
Company Queries II | Distance Queries |
Counting Paths | Subtree Queries |
Path Queries | Path Queries II |
Distinct Colors | Finding a Centroid |
Fixed-Length Paths I | Fixed-Length Paths II |
Prüfer Code | Tree Traversals |
Tree Isomorphism I | Tree Isomorphism II |
OI 暴雷
IOI 2012 D2 pA
Subordinates | Tree Matching |
Tree Diameter | Tree Distances I |
Tree Distances II | Company Queries I |
Company Queries II | Distance Queries |
Counting Paths | Subtree Queries |
Path Queries | Path Queries II |
Distinct Colors | Finding a Centroid |
Fixed-Length Paths I | Fixed-Length Paths II |
Prüfer Code | Tree Traversals |
Tree Isomorphism I | Tree Isomorphism II |
有一個長度為 \(N\) 的環狀序列,你想要把他們分成最少份,讓每份總和都 \(\leq K\)
一個想法是枚舉所有位置當起點開始跳,跳回來就結束了,但這樣是 \(O(N^2)\)
假設 \(i\) 是某塊的起點
那一定會想要拿越多越好
對於每個位置 \(i\),我們知道如果他當起點時,下一塊、下兩塊、下四塊 ... 的起點會是哪裡
利用倍增加速!
枚舉每個起點,分別倍增,複雜度 \(O(NlogN)\)
CF 1142B
CSES 1664
TIOJ 2348
USACO '23 Jan Plat pA (好題)
OI 暴雷
JOI 本選 '22 pD
BOI '22 D1 pB
Subordinates | Tree Matching |
Tree Diameter | Tree Distances I |
Tree Distances II | Company Queries I |
Company Queries II | Distance Queries |
Counting Paths | Subtree Queries |
Path Queries | Path Queries II |
Distinct Colors | Finding a Centroid |
Fixed-Length Paths I | Fixed-Length Paths II |
Prüfer Code | Tree Traversals |
Tree Isomorphism I | Tree Isomorphism II |
ABC 294G
CF 2002D2
CSA Trees Partition
NTUCPC 104
CF 1555F
CF 893F
OI 暴雷
全國賽 '21 pG
JOISC '23 D1 pA
IZHO '23 D1 pB
COI '22 pD
Subordinates | Tree Matching |
Tree Diameter | Tree Distances I |
Tree Distances II | Company Queries I |
Company Queries II | Distance Queries |
Counting Paths | Subtree Queries |
Path Queries | Path Queries II |
Distinct Colors | Finding a Centroid |
Fixed-Length Paths I | Fixed-Length Paths II |
Prüfer Code | Tree Traversals |
Tree Isomorphism I | Tree Isomorphism II |
ABC 359G
ABC 340G
CF 600E
CF 570D
OI 暴雷
BOI '17 D1 pB (再寫一次)
BOI '24 D1 pA
BOI '22 D2 pB (這個算嗎)
USACO Platinum '24 Open pA
複雜度更直觀的證明:對於兩點 \((a,b)\),只會在他們的 LCA 合併到,所以總共會有 \(O(N^2)\) 次合併
Subordinates | Tree Matching |
Tree Diameter | Tree Distances I |
Tree Distances II | Company Queries I |
Company Queries II | Distance Queries |
Counting Paths | Subtree Queries |
Path Queries | Path Queries II |
Distinct Colors | Finding a Centroid |
Fixed-Length Paths I | Fixed-Length Paths II |
Prüfer Code | Tree Traversals |
Tree Isomorphism I | Tree Isomorphism II |
COCI '19 Dzumbus
Subordinates | Tree Matching |
Tree Diameter | Tree Distances I |
Tree Distances II | Company Queries I |
Company Queries II | Distance Queries |
Counting Paths | Subtree Queries |
Path Queries | Path Queries II |
Distinct Colors | Finding a Centroid |
Fixed-Length Paths I | Fixed-Length Paths II |
Prüfer Code | Tree Traversals |
Tree Isomorphism I | Tree Isomorphism II |
CF 1254D (超酷)
ABC 351G
Library Checker Vertex Set Path Composite
CF 1017G
TIOJ 1910
OI 暴雷
CEOI '19 D1 pB
(我只會重剖 有人要上台講樹剖解嗎)
JOISC '23 D3 pC
(我只會樹剖 有人要上台講重剖解嗎)
P.S. 重心分治很多時候是在「算經過某個點的答案」比較好算時,利用重心的性質去達到這件事
給一棵 \(N\) 個點的樹,請在上面找 \(\lfloor\frac{N}{2}\rfloor\) 個點對,讓所有點對的距離和最大
回想一下前面常用的式子
\(dist(u,v)=dep_u+dep_v-2\cdot dep_{lca(u,v)}\)
如果定根在某個點,那前兩項會是固定的,所以我們想要最小化 \(dep_{lca(u,v)}\)
有沒有辦法讓所有 \(lca(u,v)\) 都在根上?
回想一下前面常用的式子
\(dist(u,v)=dep_u+dep_v-2\cdot dep_{lca(u,v)}\)
如果定根在某個點,那前兩項會是固定的,所以我們想要最小化 \(dep_{lca(u,v)}\)
有沒有辦法讓所有 \(lca(u,v)\) 都在根上?
重心定義:沒有一個子樹大小超過 \(\lfloor\frac{N}{2}\rfloor\)
所以如果定根在重心,一定可以讓 lca 都在根上 => 算前兩項和就好
Subordinates | Tree Matching |
Tree Diameter | Tree Distances I |
Tree Distances II | Company Queries I |
Company Queries II | Distance Queries |
Counting Paths | Subtree Queries |
Path Queries | Path Queries II |
Distinct Colors | Finding a Centroid |
Fixed-Length Paths I | Fixed-Length Paths II |
Prüfer Code | Tree Traversals |
Tree Isomorphism I | Tree Isomorphism II |
CF 321C
OI 暴雷
JOISC '20 D4 pA
IOI '11 D1 pB
線段樹:存分治的結果
重心樹:存重心分治的結果
樹的重心是重心樹的根,第一層分治的重心會是根的子節點,以此類推
對每個重心樹上的點維護他與子樹中黑點的最短距離
修改:在重心樹往上爬,取 min
詢問:在重心樹上往上爬,順便算答案
每個點到祖先的實際距離可以在重心分治的時候算,所以複雜度是 \(O((N+Q)logN)\)
OI 暴雷
CEOI '19 D1 pB
(我只會重剖 有人要上台講樹剖解嗎)
JOISC '23 D3 pC
(我只會樹剖 有人要上台講重剖解嗎)
Subordinates | Tree Matching |
Tree Diameter | Tree Distances I |
Tree Distances II | Company Queries I |
Company Queries II | Distance Queries |
Counting Paths | Subtree Queries |
Path Queries | Path Queries II |
Distinct Colors | Finding a Centroid |
Fixed-Length Paths I | Fixed-Length Paths II |
Prüfer Code | Tree Traversals |
Tree Isomorphism I | Tree Isomorphism II |