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 (ai≤ i). 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:

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







#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
using namespace std;
#define up(i,x,y) for(i=x;i<=y;i++)
#define up2(i,x,y) for(i=x;y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define down2(i,x,y) for(i=x;y;i--)
#define pi acos(-1.0)
#define ll long long
#define s(a) scanf("%d",&a)
#define mem(a,b) memset(a,b,sizeof(a))
#define ads(a) (a<0?-a:a)
#define w(a) while(a)
int vis[100005],dp[100005],n,a[100005];
vector<int>g[100005]; int dfs(int x)
{
if(dp[x]!=-1)
return dp[x];
int l=g[x].size();
if(!l)
dp[x]=1;
else if(l==1)
dp[x]=dfs(g[x][0]);
else
dp[x]=min(max(dfs(g[x][0])+1,dfs(g[x][1])),max(dfs(g[x][0]),dfs(g[x][1])+1));
return dp[x];
} int main()
{
int i,j;
w(~s(n))
{
up(i,0,n)
{
dp[i]=-1;
g[i].clear();
}
up(i,2,n)
{
s(a[i]);
g[a[i]].push_back(i);
}
printf("%d\n",dfs(1));
}
}

ZOJ3805:Machine的更多相关文章

  1. 【Machine Learning】KNN算法虹膜图片识别

    K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

  2. 【Machine Learning】Python开发工具:Anaconda+Sublime

    Python开发工具:Anaconda+Sublime 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...

  3. 【Machine Learning】机器学习及其基础概念简介

    机器学习及其基础概念简介 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

  4. 【Machine Learning】决策树案例:基于python的商品购买能力预测系统

    决策树在商品购买能力预测案例中的算法实现 作者:白宁超 2016年12月24日22:05:42 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本 ...

  5. 【机器学习Machine Learning】资料大全

    昨天总结了深度学习的资料,今天把机器学习的资料也总结一下(友情提示:有些网站需要"科学上网"^_^) 推荐几本好书: 1.Pattern Recognition and Machi ...

  6. [Machine Learning] Active Learning

    1. 写在前面 在机器学习(Machine learning)领域,监督学习(Supervised learning).非监督学习(Unsupervised learning)以及半监督学习(Semi ...

  7. [Machine Learning & Algorithm]CAML机器学习系列2:深入浅出ML之Entropy-Based家族

    声明:本博客整理自博友@zhouyong计算广告与机器学习-技术共享平台,尊重原创,欢迎感兴趣的博友查看原文. 写在前面 记得在<Pattern Recognition And Machine ...

  8. CPU acceleration status: HAXM is not installed on this machine解决方法

    报错信息: Starting emulator for AVD 'old_android' emulator: WARNING: Classic qemu does not support SMP. ...

  9. Azure PowerShell (6) 设置单个Virtual Machine Endpoint

    <Windows Azure Platform 系列文章目录> 请注意: - Azure不支持增加Endpoint Range - 最多可以增加Endpoint数量为150 http:// ...

随机推荐

  1. C# - InnerList

    运行效果: 代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; name ...

  2. 【linux kernel】 softirq 软中断讨论

    欢迎转载,转载时需保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http:// ...

  3. RESTFul Shiro

    RESTFul与服务没有关系?REST的本质是设计风格,不是技术. REST的URL还是个URL,就是个普通的URL,访问这个URL的时候,先被Servlet Filter(即Shiro 的Filte ...

  4. 基于visual Studio2013解决面试题之1405归并排序

     题目

  5. 基于visual Studio2013解决C语言竞赛题之1033数字交换

          题目 解决代码及点评 /* 功能:将一个一维数组中的偶数依次交换.例如有8个元素, 若其中第1.4.5三元素是偶数时应按下图交换. 例子: a[]={2,3,1,6 ...

  6. SRM589 DV1 250 回文字符串

    其实这道题挺简单的,不过刚开始我钻了一个错误的死胡同.想明白之后才发现. 题目要求用最少的时间来将一个字符串变成回文字符串.(具体题目参看topcoder srm589 DV1 250分值的题目,不便 ...

  7. 解决java mail发送TXT附件被直接显示在正文中的问题

    这两天遇到一个问题,关于使用java mail发送邮件的问题. 详细是这样子的:我使用java mail发送异常报告邮件,邮件中有一个包含异常日志的附件,和关于设备信息的邮件正文.假设日志为log后缀 ...

  8. JQuery AJAX Demo

    JQuery AJAX Demo APP发展集团:347072638(HTML5,APP) 1.先看一个JQuery AJAX Demo HTML端: <!DOCTYPE html PUBLIC ...

  9. Windows下实战Apache+PHP [转]

        一.Apache 1.下载登陆Apache Lougne(http://www.apachelounge.com/download/),找到最新版本的Apache.笔者下载的是带IPv6和Cr ...

  10. 基于visual Studio2013解决面试题之0601二叉树深度

     题目