码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ3805:Machine

时间:2014-08-26 15:37:36      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:zoj

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.

bubuko.com,布布扣

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 :

bubuko.com,布布扣

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:

bubuko.com,布布扣

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));
    }
}


ZOJ3805:Machine

标签:zoj

原文地址:http://blog.csdn.net/libin56842/article/details/38846675

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!