Machine Time limit: 2 Seconds Memory Limit: 65536 KB
In a typical assembly line, machines is connected one by one. The first machine ' s output product is the second machine's raw material. To simplify the problem, we put all machines into a two-dimension shelf. Every machine occupied exactly one grid and have both input ports and only one output port. One input port can get material from only one machine.
Pipes'll is used to connect between these machines. There is kinds of pipes: ' I ' kind and ' L ' kind. We should notice that the ' I ' kind pipe can is linked one by one. Each pipe would also occupied one grid.
In Bob's factory, each machine would get raw materials from zero, one or both other machines. Some machines don ' t need any input materials, but any machine must has an output. Machines is coded by numbers from 1 to n . The output of the machines with greater code can is the input of the machines with less code. The machine ' s output product would be the final product, and'll is not being any of other machine s input. Bob ' s factory has a shelf with infinite height, but finite width. He'll give you the dependency relationship of these machines, and want you to arrange these machines and pipes so that H E can minimize the width of the shelf.
Here's an example for your help understand:
Products would falling from higher machine to lower machine through the pipes. Here, machine 1 gets materials from machine 2 and machine 3. The whole width of this system is 2.
Input
For each case, the first line would be a integer indicates the number of the n machines (2≤n≤10000). The following line would include n-1 numbers. The-th number means that the output of machine would be is the input of machine i ai i+1 ai ( ai ≤ i ). The same code'll is appeared at most twice. Notice Machine 1 's output would be the final output, and won ' t is any machine's input.
Output
For each case, we need exactly one integer as output, which is the minimal width of the shelf.
Sample Input
31 171 1 2 2 3 3
Sample Output
23
Hint
Case 1 is the example.
Case 2:
Test instructions: There are n different machines, their relations of production constitute a binary tree, requires the use of pipelines to connect them to complete the production line, and make the overall width of the smallest, the pipeline only L-type and linear type two (specific shape see title), can not rotate the pipe. Each machine has a maximum of 2 input ports and one output outlet.
#include <cstdio> #include <cstring> #include <algorithm> #include <vector>using namespace std; const int N = 1e4 + 5;vector <int> v[n];int dp[n];void dfs (int u) { int sz = V[u].size (); if (sz = = 0) Dp[u] = 1; else if (sz = = 1) { DFS (v[u][0]); Dp[u] = dp[v[u][0]]; } else { int s1 = v[u][0], s2 = v[u][1]; DFS (s1); DFS (S2); if (dp[s1] = = Dp[s2]) dp[u] = dp[s1] + 1; else Dp[u] = max (DP[S1], dp[s2]);} } int main () { int n, A; while (~SCANF ("%d", &n)) {for (int i = 1; I <= n; i++) v[i].clear (); for (int i = 2; I <= n; i++) { scanf ("%d", &a); V[a].push_back (i); } DFS (1); printf ("%d\n", dp[1]); } return 0;}
ZOJ 3805 Machine (simple DP)