|
|
|
@ -14,15 +14,26 @@ import java.io.StreamTokenizer;
|
|
|
|
|
|
|
|
|
|
public class Code03_BiggestBSTTopologyInTree {
|
|
|
|
|
|
|
|
|
|
public static int MAXN = 200001;
|
|
|
|
|
|
|
|
|
|
public static int[][] tree = new int[MAXN][3];
|
|
|
|
|
|
|
|
|
|
public static int[] record = new int[MAXN];
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
|
|
|
|
StreamTokenizer in = new StreamTokenizer(br);
|
|
|
|
|
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
|
|
|
|
|
while (in.nextToken() != StreamTokenizer.TT_EOF) {
|
|
|
|
|
int n = (int) in.nval;
|
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
|
tree[i][0] = 0;
|
|
|
|
|
tree[i][1] = 0;
|
|
|
|
|
tree[i][2] = 0;
|
|
|
|
|
record[i] = 0;
|
|
|
|
|
}
|
|
|
|
|
in.nextToken();
|
|
|
|
|
int h = (int) in.nval;
|
|
|
|
|
int[][] tree = new int[n + 1][3];
|
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
|
in.nextToken();
|
|
|
|
|
int c = (int) in.nval;
|
|
|
|
@ -35,7 +46,7 @@ public class Code03_BiggestBSTTopologyInTree {
|
|
|
|
|
tree[c][1] = l;
|
|
|
|
|
tree[c][2] = r;
|
|
|
|
|
}
|
|
|
|
|
out.println(maxBSTTopology(h, tree, new int[n + 1]));
|
|
|
|
|
out.println(maxBSTTopology(h));
|
|
|
|
|
out.flush();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -44,37 +55,26 @@ public class Code03_BiggestBSTTopologyInTree {
|
|
|
|
|
// t: 代表树 t[i][0]是i节点的父节点、t[i][1]是i节点的左孩子、t[i][2]是i节点的右孩子
|
|
|
|
|
// m: i节点为头的最大bst拓扑结构大小 -> m[i]
|
|
|
|
|
// 返回: 以h为头的整棵树上,最大bst拓扑结构的大小
|
|
|
|
|
public static int maxBSTTopology(int h, int[][] t, int[] m) {
|
|
|
|
|
public static int maxBSTTopology(int h) {
|
|
|
|
|
if (h == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int l = t[h][1];
|
|
|
|
|
int r = t[h][2];
|
|
|
|
|
int c = 0;
|
|
|
|
|
int p1 = maxBSTTopology(l, t, m);
|
|
|
|
|
int p2 = maxBSTTopology(r, t, m);
|
|
|
|
|
while (l < h && m[l] != 0) {
|
|
|
|
|
l = t[l][2];
|
|
|
|
|
int l = tree[h][1];
|
|
|
|
|
int r = tree[h][2];
|
|
|
|
|
int p1 = maxBSTTopology(l);
|
|
|
|
|
int p2 = maxBSTTopology(r);
|
|
|
|
|
int tmp = l;
|
|
|
|
|
while (tmp < h && record[tmp] != 0) {
|
|
|
|
|
tmp = tree[tmp][2];
|
|
|
|
|
}
|
|
|
|
|
if (m[l] != 0) {
|
|
|
|
|
c = m[l];
|
|
|
|
|
while (l != h) {
|
|
|
|
|
m[l] -= c;
|
|
|
|
|
l = t[l][0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (r > h && m[r] != 0) {
|
|
|
|
|
r = t[r][1];
|
|
|
|
|
}
|
|
|
|
|
if (m[r] != 0) {
|
|
|
|
|
c = m[r];
|
|
|
|
|
while (r != h) {
|
|
|
|
|
m[r] -= c;
|
|
|
|
|
r = t[r][0];
|
|
|
|
|
}
|
|
|
|
|
record[l] -= record[tmp];
|
|
|
|
|
tmp = r;
|
|
|
|
|
while (tmp > h && record[tmp] != 0) {
|
|
|
|
|
tmp = tree[tmp][1];
|
|
|
|
|
}
|
|
|
|
|
m[h] = m[t[h][1]] + m[t[h][2]] + 1;
|
|
|
|
|
return Math.max(Math.max(p1, p2), m[h]);
|
|
|
|
|
record[r] -= record[tmp];
|
|
|
|
|
record[h] = record[l] + record[r] + 1;
|
|
|
|
|
return Math.max(Math.max(p1, p2), record[h]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|