1007 Red-black Tree (35 分)
解题思路:
红黑树居然是DP,这合理吗?
定义一个special red-black Tree,叫他r树,其与题中的red-black树区别在于根节点是红色的。
定义r[n][h]为 internal node为n,black-height=h的r树的总情况数,b[n][h]为 internal node为n,black-height=h的red-black树的总情况数
这样对于一个 internal node为n的r树,若其black-height=h,则因为其子树都只能是red-black树,所以它的构造数为b[i][h]*b[n-i-1][h],1<i<n-1,可以理解为剩下的n-1个内部节点任何分配到左右两个red-black树中,而因为结点是红色的,所以不会减少h.
那么对于一个 internal node为n的red-black树,若其black-height=h,因为其儿子结点没有限制,所以可以是r树可以是red-black树,所以不难得出情况数是:(b[i][h-1]+r[i][h-1])*(b[n-i-1][h-1]+r[n-i-1][h-1]),1<i<n-1。
同时添加初始条件:
b[1][1] = 1;
b[2][1] = 2;
b[3][1] = 1;
结合上面的式子:
- b[n][h]=(b[i][h-1]+r[i][h-1])*(b[n-i-1][h-1]+r[n-i-1][h-1])
- r[n][h]=b[i][h]*b[n-i-1][h]
最后统计计算b[n][i]即可
代码:
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define G 6.67430*1e-11
#define rd read()
#define pi 3.1415926535
using namespace std;
const ll mod = 1e9 + 7;
const int MAXN = 30000005;
const int MAX2 = 300005;
inline ll read() {
ll x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
ll fpow(ll a, ll b)
{
ll ans = 1;
while (b)
{
if (b & 1)ans = ans * a % mod;
b >>= 1;
a = a * a % mod;
}
return ans;
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll jc[505];
ll C(int n, int m)
{
if (n == 0)return 0;
if (m == 0)return 1;
if (n == m)return 1;
if (n < m)return 0;
return jc[n] * fpow(jc[m], mod - 2) % mod * fpow(jc[n - m], mod - 2);
}
ll b[505][20], r[505][20];
signed main()
{
int n = rd;
b[1][1] = 1;
b[2][1] = 2;
b[3][1] = 1;
for (int k = 1; k <= 19; k++)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < i - 1; j++)
{
b[i][k] = (b[i][k] + (r[j][k - 1] + b[j][k - 1]) * (r[i - j - 1][k - 1] + b[i - j - 1][k - 1]) % mod) % mod;
r[i][k] = (r[i][k] + b[j][k] * b[i - j - 1][k] % mod) % mod;
}
}
}
ll ans = 0;
for (int i = 1; i <= 19; i++)
{
ans = (ans + b[n][i]) % mod;
}
cout << ans;
return 0;
}