朴素 dp 可以看其他题解,这里给出一种 bitset 爆标做法。
考虑到在 dp 时会有大量的或运算,所以使用 bitset 进行优化,时间瓶颈在 $l_v=l_u$ 时的树上背包。
朴素合并的时间复杂度为:
$$ \\sum_{u} \\sum_{v \\in child(u)} (siz[u_{cur}] \\times siz[v])=\\sum_{pairs(x,y)} 1=O(n^2) $$
因为 stl 中的 bitset 只能支持整体或,实际时间复杂度比朴素背包更劣。
考虑手写 bitset,需要支持在或时只或到一个范围。
时间复杂度为 $O(\\frac{N^2}{w})$。
“`cpp #include <bits/stdc++.h> using namespace std;
template<size_t N> struct FastBitset {
static const int MAX_M=(N+63)>>6; unsigned long long a[MAX_M]; FastBitset() { memset(a,0,sizeof a); } void set(int p,int v) {
if(v) a[p>>6]|=(1ull<<(p&63)); else a[p>>6]&=~(1ull<<(p&63)); } bool test(int p) const {
&nbs


