ZOJ-3805-Machine

来源:互联网 发布:淘宝孕妇套装 编辑:程序博客网 时间:2024/06/05 02:24

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:
这里写图片描述

题目链接:ZOJ 3805

题目思路:dfs,可知,水管只有L型和直线型。

以下是代码:

#include <iostream>#include <algorithm>#include <cstring>#include <string>#include <vector>#include <map>#include <set>#include <cstdio>#include <cmath>#include <stack>#include <cstdlib>using namespace std;vector <int> v[10010];int dp[10010]; void dfs(int u){    int len = v[u].size();   //表示该方格直接相连的方格个数    if (len == 0)   dp[u] = 1;  //如果没有直接相连的方格,那么该方格宽度为1    else if (len == 1)    //有一个直接相连的方格,该方格宽度等于子方格的宽度    {        dfs(v[u][0]);        dp[u] = dp[v[u][0]];    }    else      //存在两个直接相连的方格    {        dfs(v[u][0]);  //求出两个子方格的宽度        dfs(v[u][1]);        int d0 = dp[v[u][0]];        int d1 = dp[v[u][1]];        if (d0 == d1)   dp[u] = d0 + 1;   //相等则该方格宽度其中一个子方格宽度+1,一定有一个连成L型        else    dp[u] = max(d0,d1);     //不相等,把宽度大的连为直线型,则该方格与其宽度大的子方格宽度相等    }}int main(){    int n;    while(cin >> n)    {        for (int i = 0; i <= n; i++)            v[i].clear();        for(int i = 2; i <= n; i++)        {            int a;            cin >> a;            v[a].push_back(i);  //i为a号方格的子方格        }         dfs(1);        cout << dp[1] << endl;    }    return 0;}
0 0