樹
Part II
CSES
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 |
- 到一個主題會先問有沒有人想聽
- 不然講題目為主
- 但我刷超少題所以根本沒題目
- 偷自己簡報應該不算偷吧
- 雖然還是有偷一點別人的
等下流程
CSES
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
哇我題目真的刷很少
CSES
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
請不要講爛梗
CSES
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
CSES
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
CSES
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
CSES
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)\) 次合併
對深度的啟發式合併
CSES
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
CSES
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 都在根上 => 算前兩項和就好
CSES
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
重心樹 / 重心剖分
類比
線段樹:存分治的結果
重心樹:存重心分治的結果
樹的重心是重心樹的根,第一層分治的重心會是根的子節點,以此類推
重心樹
性質
- 重心分治時,我們處理到 \(v\) 時,他會影響到的節點們就會是在重心樹上的子樹
- 重心樹上 \(v\) 的祖先,就是當重心時影響到 \(v\) 的點
- 重心樹的深度是 \(O(logn)\)
- 修改時可以暴力修改!
例題
例題
例題
例題
對每個重心樹上的點維護他與子樹中黑點的最短距離
修改:在重心樹往上爬,取 min
詢問:在重心樹上往上爬,順便算答案
每個點到祖先的實際距離可以在重心分治的時候算,所以複雜度是 \(O((N+Q)logN)\)
更多習題
OI 暴雷
更多習題
CEOI '19 D1 pB
(我只會重剖 有人要上台講樹剖解嗎)
JOISC '23 D3 pC
(我只會樹剖 有人要上台講重剖解嗎)
CSES
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 |
樹(校培)
By ck1110530
樹(校培)
- 142