zoj 3805 Machine
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 (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:

#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的更多相关文章
- ZOJ 3805 Machine(二叉树,递归)
题意:一颗二叉树,求 “ 宽度 ” 思路:递归,貌似这个思路是对的,先记下,但是提交时超时, 1.如果当前节点只有左孩子,那么当前宽度等于左孩子宽度 2.如果当前节点只有右孩子,那么当前宽度等于 ...
- ZOJ 3805 (树形DP)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5337 题目大意:方块连接,呈树形.每个方块有两种接法,一种接在父块 ...
- ZOJ 1364 Machine Schedule(二分图最大匹配)
题意 机器调度问题 有两个机器A,B A有n种工作模式0...n-1 B有m种工作模式0...m-1 然后又k个任务要做 每一个任务能够用A机器的模式i或b机器的模式j来完毕 机器開始都处于模式0 每 ...
- ZOJ 3324 Machine
线段树,延迟标记. 记录一下每个节点代表的区间的最小值,以及左右端点是否为最小值,记录区间被下压几次作为延迟标记,再记录一下这个区间中有多少个最小值的连通块. $n$最大有$1$亿,可以开动态线段树避 ...
- zoj 3325 Machine(线段树)
题意:0~n-1的数组,初始值为0:执行m个操作,每次操作执行后输出当前值为0的连续段的段数. 操作1: p i j : i~j区间的每个元素值减1 操作2: r i j :i~j区间的每个元素值加1 ...
- ZOJ Monthly, March 2018 题解
[题目链接] A. ZOJ 4004 - Easy Number Game 首先肯定是选择值最小的 $2*m$ 进行操作,这些数在操作的时候每次取一个最大的和最小的相乘是最优的. #include & ...
- HZNU Training 4 for Zhejiang Provincial Collegiate Programming Contest 2019
今日这场比赛我们准备的题比较全面,二分+数论+最短路+计算几何+dp+思维+签到题等.有较难的防AK题,也有简单的签到题.为大家准备了一份题解和AC代码. A - Meeting with Alien ...
- 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 ...
- ZOJ 3407 Doraemon's Cake Machine [数学]
题意: 最多有2000组测试样例,每组样例代表n,m; n代表要把蛋糕平分的份数,m代表必须进行多少次操作. 一共有三种操作 1.竖切 经过蛋糕圆心,将蛋糕整个向下切. 2.横切 平行于蛋糕平 ...
随机推荐
- PHP pear安装出现 Warning: require_once(Structures/Graph.php)...错误
今天在WINDOWS安装pear,一路无阻很顺利安装完成,接着想安装下pear email包来玩下,但接下来却报: Warning: require_once(Structures/Graph.php ...
- jeesite 一对多,对子表单独修改时出现 HV000030: No validator could be found for type:
这是由于springmvc校验功能造成的,解决办法参见我的百度回答 http://zhidao.baidu.com/question/2141673418419314468
- javax.el.PropertyNotFoundException: Property 'aDesc' not found on type
这个问题是是在我使用jeesite自动代码是产生的,原因是实体类的属性命名规范不合格,我在网上看到类的属性前三个字母不能出现大写 解决办法:将类的属性大小写改一下
- 10.10_魔兽账号,OSC代码托管演示,研究SQL别忘记了,git
(1)juedui8456juedui456chixin0769魔兽世界账号112288 (2)EasyXls.开源中国推出 PaaS@OSC 代码演示和运行平台.git.oschina.coding ...
- LAMP 环境 快速安装
(一)安装Apache 1.下载安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 yum install zlib-devel -y wget http://m ...
- 尝试一下用MARKDOWN嵌入代码
public void test(){ // }
- eclipse 启动 出现Failed to create the Java Virtual Machine" 解决方案
方法如下: 找到eclipse目录下的eclipse.ini,可以看到如下内容:-startupplugins/org.eclipse.equinox.launcher_1.1.0.v20100507 ...
- python 数据类型(元组(不可变列表),字符串
元组(不可变列表) 创建元组: ages = (11, 22, 33, 44, 55) 或 ages = tuple((11, 22, 33, 44, 55)) 一般情况下是不需要被人该的数值才使用元 ...
- django 自定义标签
Context和RequestContext都能被Template 进行render RequestContext类接受reqest,字典,processors 实例化.并被TEMPLATE_CONT ...
- Quartz2D学习笔记(1)
********************************** 简介 *************************************** Quartz2D是⼀个二维绘图引擎,同时支持 ...