Machine - ZOJ 3805 二叉树

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 (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:


思路:可能是dp做多了,我按照树形dp的思路去想的,每次得到两个子节点的宽度,然后得到这个节点的宽度。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
vector<int> vc[10010];
int width[10010],n;
void dfs(int v)
{ int i,j,k,len=vc[v].size();
  if(len==0)
   width[v]=0;
  else if(len==1)
  { dfs(vc[v][0]);
    width[v]=width[vc[v][0]];
  }
  else
  { dfs(vc[v][0]);
    dfs(vc[v][1]);
    width[v]=min(width[vc[v][0]],width[vc[v][1]])+1;
    if(width[vc[v][1]]>width[v])
     width[v]=width[vc[v][1]];
    if(width[vc[v][0]]>width[v])
     width[v]=width[vc[v][0]];
  }

}
int main()
{ int T,t,n,m,i,j,k,u;
  while(~scanf("%d",&n))
  { for(i=0;i<=n;i++)
     vc[i].clear();
    memset(width,0,sizeof(width));
    for(i=2;i<=n;i++)
    { scanf("%d",&u);
      vc[u].push_back(i);
    }
    dfs(1);
    printf("%d\n",width[1]+1);
  }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值