Topic Link ~~>
To do the idea : the game thought of the method, but did not have AC, unfortunately, did not take into account the right side of the sub-tree of equal situation, the result of the cup has, after the game to smooth a bit of ideas on AC, the game is not calm, the mind is not clear, the future to pay attention.
Problem Solving Ideas:
The topic is a tree, just to put the right node more on the left son on the good, so reduce the number, recursion, pay attention to the two sons of the same situation need to add 1.
Code:
#include <iostream> #include <sstream> #include <map> #include <cmath> #include <fstream> #include <queue> #include <vector> #include <sstream> #include <cstring> #include <cstdio > #include <stack> #include <bitset> #include <ctime> #include <string> #include <cctype > #include <iomanip> #include <algorithm>using namespace std, #define INT __int64#define L (x) (x * 2) #define R (x) (x * 2 + 1) const int INF = 0X3F3F3F3F; Const double ESP = 0.0000000001; const double PI = ACOs ( -1.0); const INT MOD = 1000000007; const int MY = (1<<5) + 5; const int MX = 100000 + 5; int n; struct node{int le, RT;} T[MX]; int dfs (int u)//Transform left and right sub-tree, calculate there are several sons {int lson = 0, Rson = 0, num = 0; if (t[u].le)//calculate left subtree Lson = DFS (t[u].le); if (T[U].RT)//Calculate right subtree Rson = DFS (T[U].RT); if (Lson < Rson)//If Zuozi right son of small less right operand subtree of the right son swap (t[u].le, t[u].rt); if (Lson = = Rson) return Lson + 1; Note Return Max (Lson, Rson);} int main () {//freopen ("Input.txt", "R", stdin); int x; while (~SCANF ("%d", &n)) {memset (t, 0, sizeof (t)); for (int i = 2; I <= n; ++i) {scanf ("%d", &x); if (t[x].le) t[x].rt = i; else T[x].le = i; } printf ("%d\n", DFS (1)); } return 0;}
ZOJ 3805 Machine