送你一棵圣诞树
题目背景:
分析:状压DP + 子集枚举
考虑转化问题:给你一棵n个点的树,现在将它用1 ~ k编号,显然两个编号相同,肯定不可能是其中一个是另一个的点分树祖先,那么肯定这两个点之间的路径上,一定有一个点分树祖先标号是小于这个标号的,那么到这里就可以有一些比较暴力的思路了,我们定义dp[u][s]表示,以u为根,子树中存在s集合中的值到u的路径上没有比它小的标号,直接暴力枚举子树,转移的复杂度是O(nk4k),考虑优化,我们对于点u,枚举u的标号,那么合并两个子树时,两个子树的状态S1,S2一定在第k位为0,在比k小的位置,两个不能同时为1,比k大的位置任意,那么对于比k小的位置,我们可以枚举S1,然后子集枚举S2,然后比k的的位置可以直接前缀和,所以时间复杂度为O(nk3k),可以通过适当剪枝加快速度。
Source:
/*
created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <bitset>
inline char read() {
static const int IN_LEN = 1024 * 1024;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}
///*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = read())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}
template<class T>
inline void W(T x) {
static int buf[30], cnt;
if (x == 0) write_char('0');
else {
if (x < 0) write_char('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) write_char(buf[cnt--]);
}
}
inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}
/*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
if (c == '-') iosig = true;
for (x = 0; isdigit(c); c = getchar())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int MAXK = 16;
const int MAXN = 50 + 10;
const int mod = 998244353;
int n, k, x, y, ans;
int dp[MAXN][1 << MAXK | 1];
std::vector<int> edge[MAXN];
inline void add(int &x, int t) {
x += t, (x >= mod) ? (x -= mod) : (x);
}
inline void add_edge(int x, int y) {
edge[x].push_back(y), edge[y].push_back(x);
}
inline void read_in() {
R(n), R(k);
for (int i = 1; i < n; ++i) R(x), R(y), add_edge(x, y);
}
inline void dfs(int cur, int fa) {
for (int p = 0; p < edge[cur].size(); ++p) {
int v = edge[cur][p];
if (v != fa) dfs(v, cur);
}
for (int c = k - 1; c >= 0; --c) {
static int temp1[1 << MAXK | 1], temp2[1 << MAXK | 1];
memset(temp1, 0, sizeof(int) * (1 << c));
temp1[0] = 1;
for (int p = 0; p < edge[cur].size(); ++p) {
memset(temp2, 0, sizeof(int) * (1 << c));
int v = edge[cur][p];
if (v != fa) {
for (int i = 0, s = (1 << c); i < s; ++i) {
if (temp1[i] == 0) continue ;
for (int j = s - 1 ^ i; ;j = (j - 1) & (s - 1 ^ i)) {
add(temp2[i | j], (long long)temp1[i] * dp[v][j] % mod);
if (j == 0) break ;
}
}
memcpy(temp1, temp2, sizeof(int) * (1 << c));
for (int i = 0, s = (1 << c); i < s; ++i)
add(dp[v][i], dp[v][i | (1 << c)]);
}
}
for (int i = 0, s = (1 << c); i < s; ++i)
add(dp[cur][i | s], temp1[i]);
}
}
inline void solve() {
dfs(1, 0), ans = 0;
for (int i = 0, s = (1 << k); i < s; ++i)
add(ans, dp[1][i]);
std::cout << ans;
}
int main() {
freopen("xmastree3.in", "r", stdin);
freopen("xmastree3.out", "w", stdout);
read_in();
solve();
return 0;
}