ZOJ 3805 Machine

来源:互联网 发布:bilibili mac电脑直播 编辑:程序博客网 时间:2024/05/16 06:05

H - Machine
Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu
SubmitStatus

Description

In a typical assembly line, machines are connected one by one. The first machine's output product will be 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 has two input ports and only one output port. One input port can get material from only one machine.

Pipes will be used to connect between these machines. There are two kinds of pipes : 'I' kind and 'L' kind. We should notice that the 'I' kind pipe can be linked one by one. Each pipe will also occupied one grid.

In Bob's factory, each machine will get raw materials from zero, one or two other machines. Some machines don't need any input materials, but any machine must have an output. Machines are coded by numbers from 1 ton. The output of the machines with greater code can be the input of the machines with less code. The machine NO.1's output product will be the final product, and will not be any other machine's input. Bob's factory has a shelf with infinite height, but finite width. He will give you the dependency relationship of these machines, and want you to arrange these machines and pipes so that he can minimize the width of the shelf.

Here's an example for you to help understand :

Products will 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 will be an integer n indicates the number of the machines (2≤ n≤ 10000). The following line will includen-1 numbers. The i-th number ai means that the output of machinei+1 will be the input of machine ai (aii). The same code will be appeared at most twice. Notice machine 1's output will be the final output, and won't be 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:

This problem contains massive input and output, please use efficient IO methods.

解析

DP 勉强算记忆化搜索  显而易见地叶子的宽度是1。非叶子节点的宽度是左儿子和右儿子中宽度较大的那一个,如果左右儿子的宽度相等就是l+1(或者说r+1)。
P.S.为什么让我想起了优化的并查集……

因为没初始化WA了好几发,我也是醉了。

#include<cstdio>#include<vector>using namespace std;vector<int> children[100100];int N;int find(int x){if(!children[x].size()) return 1;int l=find(children[x].at(0)),r=0;if(children[x].size()==2) r=find(children[x].at(1));if(l==r) return l+1;return l>r?l:r;}int main(){while(scanf("%d",&N)==1){for(int i=0;i<=N;i++) children[i].clear();for(int i=2;i<=N;i++){int a; scanf("%d",&a);children[a].push_back(i);}printf("%d\n",find(1));}while(1);return 0;}


0 0
原创粉丝点击