ZOJ 3805 Machine
来源:互联网 发布:bilibili mac电脑直播 编辑:程序博客网 时间:2024/05/16 06:05
Description
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 ton. 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 includen-1 numbers. The i-th number ai means that the output of machinei+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.
DP 勉强算记忆化搜索 显而易见地叶子的宽度是1。非叶子节点的宽度是左儿子和右儿子中宽度较大的那一个,如果左右儿子的宽度相等就是l+1(或者说r+1)。
P.S.为什么让我想起了优化的并查集……
因为没初始化WA了好几发,我也是醉了。
#include<cstdio>#include<vector>using namespace std;vector<int> children[100100];int N;int find(int x){if(!children[x].size()) return 1;int l=find(children[x].at(0)),r=0;if(children[x].size()==2) r=find(children[x].at(1));if(l==r) return l+1;return l>r?l:r;}int main(){while(scanf("%d",&N)==1){for(int i=0;i<=N;i++) children[i].clear();for(int i=2;i<=N;i++){int a; scanf("%d",&a);children[a].push_back(i);}printf("%d\n",find(1));}while(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
- 通过 JGit 访问 Git 仓库
- Android实现推送方式解决方案
- iap包的提交
- 使用 DrMemory 发现内存编程错误
- 走爪坐撞嘴灼诅缀庄钻揍灼坠坠椎孜灼醉
- ZOJ 3805 Machine
- 拽抓祖爪紫庄啄渍注揍祖紫缀左浊专赘锥
- codeforces 467CGeorge and Job
- 抓座做邹祖转坐诅坠资总纵作自驻宗驻总
- placeholder
- Eclipse安装WindowBuilder
- 第一次写博客
- 头文件的包含以及命名空间的引入尽量写在cpp里
- .net CHARTING图表控件下载地址