zoukankan      html  css  js  c++  java
  • 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 (aii). 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:

    树的最小款对,子树与父亲的结点相同的时候则结点树+1,不同取最大值。
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    int dp[40005],len,head[40005];
    struct node
    {
        int now,next;
    }tree[40005];
    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!=-1;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]+1;
            dp[root]=flag;
        }
        if(dp[root]==0)
          dp[root]=1;
    }
    int main()
    {
      int n,a,i;
      while(scanf("%d",&n)>0)
      {
            len=0;
          memset(dp,0,sizeof(dp));
           memset(head,-1,sizeof(head));
          for(i=2;i<=n;i++)
          {
              scanf("%d",&a);
              add(i,a);
              add(a,i);
          }
          dfs(1,-1);
          printf("%d
    ",dp[1]);
      }
    }
  • 相关阅读:
    FileReader:读取本地图片文件并显示
    uploadfy插件结合php案例
    php 生成二维码,图片上传到又拍云
    php get/post 请求(可用于请求api)获取手机号码归属地
    php中curl的详细解说
    聊聊Web App、Hybrid App与Native App的设计差异
    我的前端之路
    使用angular.js开发的一个简易todo demo
    在线个人简历(续)
    在线个人简历
  • 原文地址:https://www.cnblogs.com/cancangood/p/3937551.html
Copyright © 2011-2022 走看看