-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy path1007_Red-black Tree (35).cpp
51 lines (46 loc) · 1.48 KB
/
1007_Red-black Tree (35).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
using namespace std;
/*
B[i][j]表示i个节点,black-height为j的root为黑色的Red-Black Tree的数量
R[i][j]表示i个节点,black-height为j的root为红色的Red-Black Tree的数量
红色节点只有两个黑色节点作为子节点
黑色节点共有红红,红黑,黑红,黑黑四种情况作为子节点
所以
对于R[i][j], 则R[i][j] += B[k][j] * B[i-1-k][j],其中 0<=k<i
对于B[i][j], 则
B[i][j] +=
R[k][j-1]*R[i-1-k][j-1] +
R[k][j-1]*B[i-1-k][j-1] +
B[k][j-1]*R[i-1-k][j-1] +
B[k][j-1]*B[i-1-k][j-1]
=(R[k][j-1] + B[k][j-1])*(R[i-1-k][j-1] + B[i-1-k][j-1])
因为节点最大数量为500,所以2^H-1 = 500, H最大不会超过8.
初始化R[1][0] = B[0][0] = 1。
最后统计Sum(B[n])
*/
typedef long long int64;
const int64 M = 1000000007LL;
int main() {
ios::sync_with_stdio(false);
int64 n;
cin >> n;
vector<vector<int64>> B(n+1, vector<int64>(9, 0LL)), R(n+1, vector<int64>(9, 0LL));
R[1][0] = B[0][0] = 1LL;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 8; j++) {
for (int k = 0; k < i; k++) {
B[i][j] += ((B[k][j-1] + R[k][j-1]) * (B[i-1-k][j-1] + R[i-1-k][j-1])) % M;
B[i][j] %= M;
R[i][j] += (B[k][j] * B[i-1-k][j]) % M;
R[i][j] %= M;
}
}
}
int64 result = 0LL;
for (auto &v : B[n]) {
result = (result + v) % M;
}
cout << result << endl;
return 0;
}