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.

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 :

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:

树的最小款对,子树与父亲的结点相同的时候则结点树+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[]);
}
}

zoj 3805 Machine的更多相关文章

  1. ZOJ 3805 Machine(二叉树,递归)

    题意:一颗二叉树,求  “  宽度  ” 思路:递归,貌似这个思路是对的,先记下,但是提交时超时, 1.如果当前节点只有左孩子,那么当前宽度等于左孩子宽度 2.如果当前节点只有右孩子,那么当前宽度等于 ...

  2. ZOJ 3805 (树形DP)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5337 题目大意:方块连接,呈树形.每个方块有两种接法,一种接在父块 ...

  3. ZOJ 1364 Machine Schedule(二分图最大匹配)

    题意 机器调度问题 有两个机器A,B A有n种工作模式0...n-1 B有m种工作模式0...m-1 然后又k个任务要做 每一个任务能够用A机器的模式i或b机器的模式j来完毕 机器開始都处于模式0 每 ...

  4. ZOJ 3324 Machine

    线段树,延迟标记. 记录一下每个节点代表的区间的最小值,以及左右端点是否为最小值,记录区间被下压几次作为延迟标记,再记录一下这个区间中有多少个最小值的连通块. $n$最大有$1$亿,可以开动态线段树避 ...

  5. zoj 3325 Machine(线段树)

    题意:0~n-1的数组,初始值为0:执行m个操作,每次操作执行后输出当前值为0的连续段的段数. 操作1: p i j : i~j区间的每个元素值减1 操作2: r i j :i~j区间的每个元素值加1 ...

  6. ZOJ Monthly, March 2018 题解

    [题目链接] A. ZOJ 4004 - Easy Number Game 首先肯定是选择值最小的 $2*m$ 进行操作,这些数在操作的时候每次取一个最大的和最小的相乘是最优的. #include & ...

  7. HZNU Training 4 for Zhejiang Provincial Collegiate Programming Contest 2019

    今日这场比赛我们准备的题比较全面,二分+数论+最短路+计算几何+dp+思维+签到题等.有较难的防AK题,也有简单的签到题.为大家准备了一份题解和AC代码. A - Meeting with Alien ...

  8. POJ 1325、ZOJ 1364、HDU 1150 Machine Schedule - from lanshui_Yang

    Problem Description As we all know, machine scheduling is a very classical problem in computer scien ...

  9. ZOJ 3407 Doraemon's Cake Machine [数学]

    题意: 最多有2000组测试样例,每组样例代表n,m; n代表要把蛋糕平分的份数,m代表必须进行多少次操作. 一共有三种操作 1.竖切   经过蛋糕圆心,将蛋糕整个向下切. 2.横切   平行于蛋糕平 ...

随机推荐

  1. map容器对象插入数据的4种方式

    #include <string> #include <iostream>  #include <map>  #include <utility>  u ...

  2. Java LoggingAPI 使用方法

    因为不想导入Log4j的jar,项目只是测试一些东西,因此选用了JDK 自带的Logging,这对于一些小的项目或者自己测试一些东西是比较好的选择. Log4j中是通过log4j.properties ...

  3. utf8转为gb2312的函数

    from:http://blog.csdn.net/qianguozheng/article/details/46429245 // 代码转换操作类 class CodeConverter { pri ...

  4. 清理SQL多余登录信息

    服务器列表.登陆帐户.密码等信息都记录在 %AppData%\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin (2008)%A ...

  5. wamp的mysql密码修改

    ==方法1== 通过WAMP打开mysql控制台,提示输入密码,因为现在是空,所以直接按回车. 输入“use mysql”,意思是使用mysql这个数据库教程,提示“Database changed” ...

  6. corosync+pacemaker and drbd实现mysql高可用集群

    DRBD:Distributed Replicated Block Device 分布式复制块设备,原理图如下 DRBD 有主双架构和双主架构的,当处于主从架构时,这个设备一定只有一个节点是可以读写的 ...

  7. 【javascript 变量和作用域】

    今天学习了javascript 的变量和作用域的基本知识,对于以前在开发中遇到的一些不懂的小问题也有了系统的认识,收获还是比较多的. [基本类型和引用类型] ECMAScript 变量可能包含两种不同 ...

  8. 列表页url参数格式分析【求指教】

    运营对列表页url制定静态化模式,与区区观点相悖.遂请大家指教点解. 动态参数包含6个,分别是: 1认证(有机),2品类(水果),3地区(丰台),4状态(众筹中),5排序(评分),6分页 使用状态非常 ...

  9. Things About 'extern'

    Note: All Learned From Here C和Objective-C的function前面都有个隐含的extern,对于function来说,有没有extern都无所谓,但变量不一样. ...

  10. MINA源码阅读之ACP

    Processor在XXAcceptor以及XXConnector中所扮演的只能就是:作为Acceptor以及Connetor所创建的Session的Processor: IoAcceptor作为他所 ...