#include <bits/stdc++.h>
using namespace std;
int main()
{
int ans = 0;
for (int k = 30; k >= 0; --k)
{
if (ans + (1 << k) <= n && check (ans + (1 << k)) == 1) ans += (1 << k);
}
return 0;
}
树形DP:给一棵无根树,树形DP通常先选一个根(常选1号点)。
DFS后,对每个结点的子树进行遍历\
转移方程,状态一般写成:dp_{i, ...} 只和 u 的子树有关。
树上背包DP
换根DP:选每一个根中的最优答案
选课,没有上司约会,LCA,拓扑排序,最大食物链数量。
拓扑排序:记录每一个节点的入度,为零即进队,作为候选答案,一直循环
```cpp
//你看, 這世界开滿了花。
#include <bits/stdc++.h>
#define LL long long
#define pb push_back
constexpr int N = 600 + 10;
LL n, in_degree[N];
std::vector<LL> edge[N];
std::queue<LL> cur;
inline LL read()
{
LL f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
while (c <= '9' && c >= '0') { x= (x << 1) + (x << 3) + (c ^ '0'); c = getchar(); }
return f * x;
}
inline void solve()
{
while (!cur.empty())
{
auto pre = cur.front(); cur.pop();
std::cout << pre << '\n';
for (auto t : edge[pre])
{
in_degree[t]--;
if (!in_degree[t]) cur.push(t);
}
}
return ;
}
int main()
{
n = read();
for (int i = 1; i <= n; ++i)
{
LL v; v = read();
while (v != 0) { edge[i].pb(v); in_degree[v]++; v = read(); }
}
for (int i = 1; i <= n; ++i)
if (!in_degree[i]) cur.push(i);
solve();
return 0;
}
```
差分约束:不会