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));
    }
}
时间: 2024-10-10 07:34:10

ZOJ3805:Machine的相关文章

【机器学习实战】Machine Learning in Action 代码 视频 项目案例

MachineLearning 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远 Machine Learning in Action (机器学习实战) | ApacheCN(apache中文网) 视频每周更新:如果你觉得有价值,请帮忙点 Star[后续组织学习活动:sklearn + tensorflow] ApacheCN - 学习机器学习群[629470233] 第一部分 分类 1.) 机器学习基础 2.) k-近邻算法 3.) 决策树 4.) 基于概率论的分类方法:朴素

How to Downgrade VMware ESXi 5.5 Virtual Machine Hardware Version to 9 from 10

Shut down the virtual machine in the vSphere client. Remove your virtual machine from the ESXi inventory. Browse your Datastore and find the virtual machine's .vmx file.   Download it to your desktop. Open the .vmx file in Notepad or your favorite te

机器学习系列(11)_Python中Gradient Boosting Machine(GBM)调参方法详解

原文地址:Complete Guide to Parameter Tuning in Gradient Boosting (GBM) in Python by Aarshay Jain 原文翻译与校对:@酒酒Angie && 寒小阳([email protected]) 时间:2016年9月. 出处:http://blog.csdn.net/han_xiaoyang/article/details/52663170 声明:版权所有,转载请联系作者并注明出 1.前言 如果一直以来你只把GBM

Machine Learning In Action 第二章学习笔记: kNN算法

本文主要记录<Machine Learning In Action>中第二章的内容.书中以两个具体实例来介绍kNN(k nearest neighbors),分别是: 约会对象预测 手写数字识别 通过“约会对象”功能,基本能够了解到kNN算法的工作原理.“手写数字识别”与“约会对象预测”使用完全一样的算法代码,仅仅是数据集有变化. 约会对象预测 1 约会对象预测功能需求 主人公“张三”喜欢结交新朋友.“系统A”上面注册了很多类似于“张三”的用户,大家都想结交心朋友.“张三”最开始通过自己筛选的

[raspberry] raspberry 充当time machine

之前是用硬盘直接当timemachine的,看到有人用raspberry+硬盘充当timemachine的 自己的也搞了下,还是蛮方便的,下面是具体的步骤 1.安装必要的服务 sudo apt-get update sudo apt-get upgrade sudo apt-get install netatalk 2. 划分硬盘 没啥特别的需求,直接不分区了,全做文件系统了 尽管可以使用hfsplus文件系统,但是由于不支持journal,放弃了避免断个电啊什么的没保护 sudo mkfs.e

【cl】解决Fail to create the java Virtual Machine

eclipse打开,提示Fail to create the java Virtual Machine 解决方法: 1.到eclipse安装目录下,找到eclipse.ini 2.按键盘ctrl+F,复制以下字符“launcher.XXMaxPermSize”,去掉引号,查找 3.将随后出现的两个216m改成128m

[Machine Learning] 国外程序员整理的机器学习资源大全

本文汇编了一些机器学习领域的框架.库以及软件(按编程语言排序). 1. C++ 1.1 计算机视觉 CCV —基于C语言/提供缓存/核心的机器视觉库,新颖的机器视觉库 OpenCV—它提供C++, C, Python, Java 以及 MATLAB接口,并支持Windows, Linux, Android and Mac OS操作系统. 1.2 机器学习 MLPack DLib ecogg shark 2. Closure Closure Toolbox—Clojure语言库与工具的分类目录 3

New to Machine Learning? Avoid these three mistakes

http://blog.csdn.net/pipisorry/article/details/43973171 James Faghmous提醒机器学习初学者要避免的三方面错误,推荐阅读 New to Machine Learning? Avoid these three mistakes Common pitfalls when learning from data Machine learning (ML) is one of the hottest fields in data scien

Machine Learning - VI. Logistic Regression (Week 3)

http://blog.csdn.net/pipisorry/article/details/43884027 机器学习Machine Learning - Andrew NG courses学习笔记 Classification  0.1表示含义 denote with 0 is the negative class denote with 1 is the positive class.  Hypothesis Representation  Decision Boundary  Cost