ZOJ3805:Machine
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的更多相关文章
- 【Machine Learning】KNN算法虹膜图片识别
		
K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...
 - 【Machine Learning】Python开发工具:Anaconda+Sublime
		
Python开发工具:Anaconda+Sublime 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...
 - 【Machine Learning】机器学习及其基础概念简介
		
机器学习及其基础概念简介 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...
 - 【Machine Learning】决策树案例:基于python的商品购买能力预测系统
		
决策树在商品购买能力预测案例中的算法实现 作者:白宁超 2016年12月24日22:05:42 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本 ...
 - 【机器学习Machine Learning】资料大全
		
昨天总结了深度学习的资料,今天把机器学习的资料也总结一下(友情提示:有些网站需要"科学上网"^_^) 推荐几本好书: 1.Pattern Recognition and Machi ...
 - [Machine Learning] Active Learning
		
1. 写在前面 在机器学习(Machine learning)领域,监督学习(Supervised learning).非监督学习(Unsupervised learning)以及半监督学习(Semi ...
 - [Machine Learning & Algorithm]CAML机器学习系列2:深入浅出ML之Entropy-Based家族
		
声明:本博客整理自博友@zhouyong计算广告与机器学习-技术共享平台,尊重原创,欢迎感兴趣的博友查看原文. 写在前面 记得在<Pattern Recognition And Machine ...
 - CPU acceleration status: HAXM is not installed on this machine解决方法
		
报错信息: Starting emulator for AVD 'old_android' emulator: WARNING: Classic qemu does not support SMP. ...
 - Azure PowerShell (6) 设置单个Virtual Machine Endpoint
		
<Windows Azure Platform 系列文章目录> 请注意: - Azure不支持增加Endpoint Range - 最多可以增加Endpoint数量为150 http:// ...
 
随机推荐
- 【linux kernel】 中断处理-中断上半部
			
欢迎转载,转载时需保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:htt ...
 - 认识到了x64程序的必要性
			
假如我做一个程序,在运行过程中需要使用一个Map,然而这个Map存储了超多信息的话,系统内存不够就会崩溃了.以前的解决方案可能是把内容存储在一个文件/数据库里,但是有内存岂不是更方便.更直截了当!
 - WCF技术剖析之十三:序列化过程中的已知类型(Known Type)
			
原文:WCF技术剖析之十三:序列化过程中的已知类型(Known Type) [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经>为此录制的节目视频(苏州话) ...
 - perl malformed JSON string, neither tag, array, object, number, string or atom, at character offset
			
[root@wx03 ~]# cat a17.pl use JSON qw/encode_json decode_json/ ; use Encode; my $data = [ { 'name' = ...
 - C++ 中使用boost::property_tree读取解析ini文件
			
boost 官网 http://www.boost.org/ 下载页面 http://sourceforge.net/projects/boost/files/boost/1.53.0/ 我下载的是 ...
 - KMP算法 KMP模式匹配 一(串)
			
A - KMP模式匹配 一(串) Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:131072KB ...
 - 杂题_POJ上的过桥问题
			
本文出自:http://blog.csdn.net/svitter 过桥问题解释:一条船能够坐两个人,可是有非常多人要过河,所以送过一个人去,还有一个人还要回来接.使全部人过河之后时间最短,怎样求? ...
 - 在C++工程中main函数之前跑代码的廉价方法(使用全局变量和全局函数)
			
// test.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...
 - java实现指定文件扫描目录
			
package com.miss.time1230.io; import java.io.File; import java.util.Scanner; /** * @author MISS * 描述 ...
 - HDU 4350 Card
			
打表找规律,比赛应付了一下,其实还可以把内存再优化一半掉,下面的0都是手动填充的,可以优化掉 题意: T个测试数据 下面52个数字表示 从栈顶到栈底的52个数 n l r表示 从栈顶下数 [l,r] ...