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.横切 平行于蛋糕平 ...
 
随机推荐
- 国庆第三天2014年10月3日10:21:39,Nutz,WebCollector,jsoup
			
(1)做得好,做得快,只能选择一样. (2)时间过得很快,你没法在假期的一天里完成更多的计划.假期全部由自己支配,相对长一点的睡眠,新加入的娱乐(视频或者游戏),你不比在工作中更有效率. (3)每天练 ...
 - hrbust 1328 相等的最小公倍数(数论)
			
Description 定义An为1,2,-,n的最小公倍数,例如,A1 = 1,A2 = 2,A3 = 6,A4 = 12,A5 = 60,A6 = 60. 请你判断对于给出的任意整数n,An是否等 ...
 - gitlab    ActionView::Template::Error (undefined method `[]' for nil:NilClass): 500错误
			
Started GET "/mygroup/myproject/tree/master/MyDirectory" for 127.0.0.1 at 2014-10-22 22:42 ...
 - win7下简单FTP服务器搭建
			
本文介绍通过win7自带的IIS来搭建一个只能实现基本功能的FTP服务器,第一次装好WIN7后我愣是没整出来,后来查了一下网上资料经过试验后搭建成功,其实原理和步骤与windows前期的版本差不多,主 ...
 - spring的基本配置
			
一:web.xml (1)spring mvc的配置 <servlet> <description>spring mvc servlet</description> ...
 - jQuery选择器(一)
			
1.#id根据给定的ID匹配一个元素. <div id="notMe"><p>id="notMe"</p></div& ...
 - 接口Interface
			
接口的定义 声明接口的方式与声明类的方式相似,但使用的关键字是interface,而不是class interface IMyInterface { // 接口成员 } 访问修饰符关键字public ...
 - J2EE中的HttpSession
			
J2EE中的HttpSession总结: ①什么是session? session是服务器端技术,利用这个技术,服务器在运行时可以为每一个浏览器创建一个共享的session对象,由于 session为 ...
 - php获取文件mime类型Fileinfo等方法
			
前几天写到使用wordpress xmlrpc api远程发布文章,如果本地服务器的文章库里某一篇待发表的wordpress文章包含图片文件时,就会使用到WordPress上传文件的API metaW ...
 - angular.js的post数据方式
			
公司的项目前端部分现在改用angular,一切从头学起,今天记录一下关于数据请求的问题,由于get的请求方式比较简单,与post也类似,所以就单独讲讲post方式. 文档上post数据的写法有好几种, ...