ZOJ 3805 Machine
来源:互联网 发布:淘宝算命害了多少人 编辑:程序博客网 时间:2024/06/05 09:37
搜索....
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 aimeans 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
31 171 1 2 2 3 3
Sample Output
23
Hint
Case 1 is the example.
Case 2:
This problem contains massive input and output, please use efficient IO methods.
Author: ZHU, Jiale; GONG, Yuan
Source: ZOJ Monthly, August 2014
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=50100;struct Edge{ int to,next;}edge[maxn*2];int Adj[maxn],Size;int n;void init(){ Size=0; memset(Adj,-1,sizeof(Adj));}void Add_Edge(int u,int v){ edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++;}int dfs(int u,int fa){int son=0,s1=0,s2=0;for(int i=Adj[u];~i;i=edge[i].next){int v=edge[i].to;if(v==fa) continue;if(son==0){s1=dfs(v,u);}else{s2=dfs(v,u);}son++;}if(son==0) return 1;else if(son==1) return s1;else return min(max(s1+1,s2),max(s1,s2+1));}int main(){ while(scanf("%d",&n)!=EOF) { init(); for(int i=2;i<=n;i++) { int x; scanf("%d",&x); Add_Edge(x,i); Add_Edge(i,x); }printf("%d\n",dfs(1,1)); } return 0;}
- zoj 3805 Machine
- ZOJ 3805 Machine
- ZOJ 3805 Machine
- ZOJ 3805 Machine
- ZOJ 3805 Machine
- ZOJ 3805 Machine
- ZOJ - 3805 Machine
- ZOJ-3805-Machine
- Machine - ZOJ 3805 二叉树
- ZOJ Machine
- ZOJ 3805 Machine(树形DP)
- ZOJ 3805 Machine(简单dp)
- ZOJ 3805 Machine(二叉树,递归)
- ZOJ-3805---Machine (记忆化dfs)
- zoj 3805 Machine(ZOJ Monthly, August 2014 - H)
- ZOJ - 3324 Machine
- ZOJ 1364 Machine Schedule
- ZOJ 3324 Machine
- Project Euler problem 479
- 二叉树遍历
- [MOOC笔记]第一章 绪论(数据结构)
- attempt to create delete event with null entity解决办法
- SQL中on条件与where条件的区别[转]
- ZOJ 3805 Machine
- hdu 1864 最大报销额 01背包
- 水仙花数
- eclipse报错:org.eclipse.swt.custom.StyledText.getScrollbarsMode()I
- 内核学习
- 笔试总结1
- Search a 2D Matrix
- 高级电脑操作就是拒绝不确定性--window运行命令大全
- 在主机Ubuntu12.04 上 安装VirtualBox,在VirtualBox上安装 12.04