首页 技术 正文
技术 2022年11月14日
0 收藏 752 点赞 3,276 浏览 2328 个字

Machine


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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.

zoj 3805             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 to n. 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 :

zoj 3805             Machine

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 include n-1 numbers. The i-th number ai means that the output of machine i+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

3
1 1
7
1 1 2 2 3 3

Sample Output

2
3

Hint

Case 1 is the example.
Case 2:

zoj 3805             Machine树的最小款对,子树与父亲的结点相同的时候则结点树+1,不同取最大值。

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int dp[],len,head[];
struct node
{
int now,next;
}tree[];
void add(int x,int y)
{
tree[len].now=y;
tree[len].next=head[x];
head[x]=len++;
}
void dfs(int root,int p)
{
int i,j,flag,son;
for(i=head[root];i!=-;i=tree[i].next)
{
son=tree[i].now;
if(son==p)
continue;
dfs(son,root);
if(dp[root]<dp[son])
flag=dp[son];
else if(dp[root]==dp[son])
flag=dp[root]+;
dp[root]=flag;
}
if(dp[root]==)
dp[root]=;
}
int main()
{
int n,a,i;
while(scanf("%d",&n)>)
{
len=;
memset(dp,,sizeof(dp));
memset(head,-,sizeof(head));
for(i=;i<=n;i++)
{
scanf("%d",&a);
add(i,a);
add(a,i);
}
dfs(,-);
printf("%d\n",dp[]);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,505
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844