-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathP2585.c
More file actions
78 lines (68 loc) · 2.51 KB
/
P2585.c
File metadata and controls
78 lines (68 loc) · 2.51 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
#define maxs 500005
int dp[maxs][3][2];
#define r 0
#define g 1
#define b 2
#define maxv 0
#define minv 1
char tree[maxs];
static size_t index = 0;
static size_t tni = 0; //tree node index
int max(int x, int y) {
return x > y ? x : y;
}
int min(int x, int y) {
return x < y ? x : y;
}
int max3(int x, int y, int z) {
return max(max(x, y), z);
}
int min3(int x, int y, int z) {
return min(min(x, y), z);
}
size_t DP() {
size_t this_tni = tni++; //this tni
char num_subtrees = tree[index++];
if (num_subtrees == '0') {
dp[this_tni][r][maxv] = dp[this_tni][r][minv] = 0;
dp[this_tni][g][maxv] = dp[this_tni][g][minv] = 1;
dp[this_tni][b][maxv] = dp[this_tni][b][minv] = 0;
goto END;
}
size_t left_tni = DP();
if (num_subtrees == '1') {
//r
dp[this_tni][r][maxv] = max(dp[left_tni][g][maxv], dp[left_tni][b][maxv]);
dp[this_tni][r][minv] = min(dp[left_tni][g][minv], dp[left_tni][b][minv]);
//g
dp[this_tni][g][maxv] = 1 + max(dp[left_tni][r][maxv], dp[left_tni][b][maxv]);
dp[this_tni][g][minv] = 1 + min(dp[left_tni][r][minv], dp[left_tni][b][minv]);
//b
dp[this_tni][b][maxv] = max(dp[left_tni][r][maxv], dp[left_tni][g][maxv]);
dp[this_tni][b][minv] = min(dp[left_tni][r][minv], dp[left_tni][g][minv]);
goto END;
}
size_t right_tni = DP();
//r
dp[this_tni][r][maxv] = max(dp[left_tni][g][maxv] + dp[right_tni][b][maxv], dp[left_tni][b][maxv] + dp[right_tni][g][maxv]);
dp[this_tni][r][minv] = min(dp[left_tni][g][minv] + dp[right_tni][b][minv], dp[left_tni][b][minv] + dp[right_tni][g][minv]);
//g
dp[this_tni][g][maxv] = 1 + max(dp[left_tni][r][maxv] + dp[right_tni][b][maxv], dp[left_tni][b][maxv] + dp[right_tni][r][maxv]);
dp[this_tni][g][minv] = 1 + min(dp[left_tni][r][minv] + dp[right_tni][b][minv], dp[left_tni][b][minv] + dp[right_tni][r][minv]);
//b
dp[this_tni][b][maxv] = max(dp[left_tni][r][maxv] + dp[right_tni][g][maxv], dp[left_tni][g][maxv] + dp[right_tni][r][maxv]);
dp[this_tni][b][minv] = min(dp[left_tni][r][minv] + dp[right_tni][g][minv], dp[left_tni][g][minv] + dp[right_tni][r][minv]);
END:
return this_tni;
}
int main() {
scanf("%s", tree);
size_t root_tni = DP();
printf("%d %d\n",
max3(dp[root_tni][r][maxv], dp[root_tni][g][maxv], dp[root_tni][b][maxv]),
min3(dp[root_tni][r][minv], dp[root_tni][g][minv], dp[root_tni][b][minv])
);
exit(0);
}