zoukankan      html  css  js  c++  java
  • ZOJ3805:Machine

    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:

    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
    ",dfs(1));
        }
    }
    


  • 相关阅读:
    Debug入门之旅StackoverFlow exception的调试
    [软件调试学习笔记]WinDbg演示IA32 CPU下的Windows 分页机制下的地址转换过程
    转载推荐:COM中不同字符类型相互转换,例如char*, BSTR, CString等等
    一种强行指定dll assembly读取其相应*.dll.config配置文件的方法(又名:如何创建.net 的DCOM)
    COM中的error handling机制及示例(ISupportEfforInfo,ICreateErrorInfo,IErrorInfo)
    (转载)如何在WCF实现impersonnate客户端的功能
    如何实现DCOM或者COM+的远程调用
    [软件调试学习笔记]防止栈缓冲区溢出的基于Cookie的安全检查机制
    关于COM的RegFree(免注册)技术简介及实例讲解。
    如何通过扩展WCF来定制和扩展WCF行为
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4297489.html
Copyright © 2011-2022 走看看