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. 类库探源——System.Configuration 配置信息处理

    按照MSDN描述 System.Configuration 命名空间 包含处理配置信息的类型 本篇文章主要两方面的内容 1. 如何使用ConfigurationManager 读取AppSetting ...

  2. 字节序转换与结构体位域(bit field)值的读取

    最近又遇到了几年前遇到的问题,标记一下. 对于跨字节位域(bit field)而言,如果数据传输前后环境的字节序不同(LE->BE,BE->LE),简单地调用(ntohs/ntohl/ht ...

  3. Sql server 浅谈用户定义表类型

    1.1 简介 SQL Server 中,用户定义表类型是指用户所定义的表示表结构定义的类型.您可以使用用户定义表类型为存储过程或函数声明表值参数,或者声明您要在批处理中或在存储过程或函数的主体中使用的 ...

  4. ubuntu tengine 安装

    参考文章:http://wangyan.org/blog/install-openssl-from-source.html http://www1.site90.com/Linux/405.html ...

  5. initrd.gz的解压和制作

    解压: gzip -d initrd.gz cpio -idmv < initrd 压缩: find . | cpio -o -c > initrd.img gzip initrd.img ...

  6. c#基础班笔记

    1.静态与非静态的区别:是否有static 非静态: 1)在非静态类中,既可以有实例成员,也可以有静态成员 2)在调用实例成员,通过  对象.实例成员 在调用静态成员时,通过  类名.静态成员 静态: ...

  7. notepad++ :正则表达式系统教程

    前言&索引 前言 正则表达式是烦琐的,但是强大的,学会之后的应用会让你除了提高效率外,会给你带来绝对的成就感.只要认真去阅读这些资料,加上应用的时候进行一定的参考,掌握正则表达式不是问题. 索 ...

  8. django之JavaScript的简单学习2

    前言:ajax预备知识:json进阶 1.JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON是用字符串来表示Javascript对象: 请大家记住一 ...

  9. 普及下Oracle hints语法

    普及下Oracle hints的语法:{DELETE|INSERT|SELECT|UPDATE} /*+ hint [text] [hint[text]]... */ 1.hint只能出现在诸如sel ...

  10. MVC5框架解析之Controller的创建

    在上一讲中我们介绍了MvcHandler,知道在Handler里面注入两个接口属性分别为IControllerFactory和IController的factory和controller.并且通过IO ...