ZOJ 3805 Machine(搜索+技巧)【树形DP模板】

本文详细介绍了ZOJ 3805题目解题过程,重点探讨了如何运用搜索和树形动态规划(DP)模板解决这一问题。通过实例解析,展示了搜索和树形DP在解决复杂问题中的关键步骤和技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

<h4< dd="">
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.


 【题解】

 这题第一次打眼一看,树形dp???当时就有点方,,但是后来看见有用搜索做的,就学了一下。

 这个题也可以作为树形DP的入门题,可以试一下哦。

 搜索做其实也蛮简单的,就是要判断一下选哪个子节点的宽度作为父节点的最小宽度,毫无疑问是大的那个。

 具体看注释吧。

 

 【AC代码】

 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int N=1e4+10;
int m,n;
int dp[N];
vector <int> vis[N];//二维数组

int dfs(int root)
{
    int s=vis[root].size();
    if(s == 0) return dp[root]=1;//当前点没有子节点  说明只有自身一个  返回1
    else if(s == 1) //只有一个子节点  则一定放在上方
    {
        dfs(vis[root][0]);//递归搜索
        dp[root]=dp[vis[root][0]];//当前节点的最小宽度等于上方子节点的最小宽度
    }
    else
    {
        int x1=vis[root][0],x2=vis[root][1];//0为当前节点上方的最小宽度  1为侧方宽度
        dfs(x1);
        dfs(x2);
        if(dp[x1] == dp[x2]) dp[root]= dp[x1]+1; //如果侧面的最小宽度与上方子节点的最小宽度相等  那么直接宽度加1 
                                                 //就像样例2的2 3节点  宽度相同  所以就相当于只增加了1个宽度
        else dp[root] = max(dp[x1] ,dp[x2]);//宽度不一样  就挑出宽度最大的作为当前节点的最小宽度
    }
}

int main()
{
    while(~scanf("%d",&m))
    {
        for(int i=1;i<=N;++i)
            vis[i].clear();
        for(int i=2;i<=m;++i)
        {
            scanf("%d",&n);
            vis[n].push_back(i);
        }
        dfs(1);//从根节点开始搜索
        printf("%d\n",dp[1]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值