Solutions
Dynamic Programming on Trees (Tree DP)Second Scanning
题意
在一棵树上,边有边权,给定 $k$ 个特殊点,对于每个点 $i$ 求,从 $i$ 出发必须经过所有特殊点的最短路径长度(可重复)。
$n,k\leq10^5$ 。
题解
不能直接对 dfn 排序然后线性做因为不对。
考虑树形 DP,答案就是从 $i$ 出发到每个有点的儿子一来一回,最终减去最长链,也可以跑到父亲那里,换根即可。对于最长链,有时需要次长链更新。
const i …
Jayun 发布于 2023-11-15 20:21:26
00
Simulated Annealing
退火。
const int N = 1e3 + 5;
inline ll Read() {
ll x = 0, f = 1;
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') f = -f, c = getchar();
whil …
Jayun 发布于 2023-11-15 16:39:38
00
Dynamic Programming on Trees (Tree DP)Second ScanningCount
题意
给出一棵大小为 $n$ 的树,和两个值 $p,q$ ,求有多少个四元组 $(a,b,c,d)$ 满足路径 $a\rightarrow b$ 的长度是 $p$ ,路径 $c\rightarrow d$ 的长度是 $q$ ,且两路径不交。
$n,p,q\leq3000$ 。
口胡
考虑容斥转化问题为求交的。交有两种情况,两条路径的 LCA 都是同一个点;一条路径穿过另一条 …
Jayun 发布于 2023-11-15 08:10:28
00
Link Cut Tree (LCT)Splay
板题,只放代码。
const int N = 3e5 + 5;
inline ll Read() {
ll x = 0, f = 1;
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') f = -f, c = getchar();
…
Jayun 发布于 2023-11-14 21:40:39
00
K-D Tree (KDT)Rotating CalipersMonotonous StackBinary Heap
KDT 板子,但被卡了。
const int N = 4e5 + 5, K = 2;
const ll inf = 1e16;
inline ll Read() {
ll x = 0, f = 1;
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c …
Jayun 发布于 2023-11-13 21:50:47
10
Virtual TreeDynamic Programming on Trees (Tree DP)Depth First Search (DFS)Binary Lifting
题意
$n$ 点树, $q$ 次询问,给 $m$ 个特殊点。树上每个点由最靠近它的特殊点控制,如果距离相同取编号小的。问每个特殊点控制多少点。
$n,q,\sum m\leq3\times10^5$ 。
题解
虚树。然后考虑树形 DP,从下往上扫一遍从上往下扫一遍。细节很多。
代码
const int N = 3e5 + 5, INF = 0x3f3f3f3f;
inline ll …
Jayun 发布于 2023-11-12 21:42:33
00
Virtual TreeDynamic Programming on Trees (Tree DP)Binary LiftingSimple Stack
题意
给定一棵树,多组询问,每组询问给定 $k$ 个点,你可以删掉不同于那 $k$ 个点的 $m$ 个点,使得这 $k$ 个点两两不连通,要求最小化 $m$ ,如果不可能输出 $-1$ 。询问之间独立。
$n,q,\sum k\leq10^5$
题解
特殊点作为关键点,将关键点按 $\mathrm{dfn}$ 排序,求出相邻点的最近公共祖先也作关键点,按照原树祖先后代关 …
Jayun 发布于 2023-11-12 20:44:28
00
2-SATStrongly Connected ComponentsTarjan
板。
const int N = 3e5 + 5;
inline ll Read() {
ll x = 0, f = 1;
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') f = -f, c = getchar();
while …
Jayun 发布于 2023-11-12 20:21:20
00
2-SATTarjanStrongly Connected Components
2-SAT 板子。
const int N = 2e6 + 10;
int n, m;
struct edge
{
int to, nxt;
}e[N << 1];
int head[N], tot;
void add (int u, int v)
{
e[++tot] = (edge){v, head[u]}, head[u] = tot;
}
int dfn[N], l …
Jayun 发布于 2023-11-12 19:46:47
00
Leftist TreeDisjoint Set
题解
并查集维护祖先,其余左偏树。
const int N = 1e5 + 5;
inline ll Read() {
ll x = 0, f = 1;
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') f = -f, c = ge …
Jayun 发布于 2023-11-12 17:48:32
00