标签:
#include "iostream" #include "algorithm" using namespace std; int parent[10001]; int depth[10001][2]; // the maximum depth of two subtree + 1 int smallIdx(int x) { if(depth[x][0] < depth[x][1]) return 0; else return 1; } int bigIdx(int x) { if(depth[x][0] < depth[x][1]) return 1; else return 0; } int main() { int m, n, maxDepth = 0; cin >> m >> n; for(int i=2; i<=m; i++) { cin >> parent[i]; } for(int i=m+1; i<=m+n; i++) { int p; cin >> p; depth[p][smallIdx(p)] = 1; } for(int i=m; i>=1; i--) { depth[parent[i]][smallIdx(parent[i])] = max(depth[i][bigIdx(i)] + 1, depth[parent[i]][smallIdx(parent[i])]); maxDepth = max(depth[i][0]+depth[i][1], maxDepth); } cout << maxDepth; }
标签:
原文地址:http://www.cnblogs.com/meelo/p/5838152.html