ZOJ 3805 Machine(简单dp)

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:

题意:有 n 个不同的机器,他们之间的生产关系构成一棵二叉树,要求使用管道把它们连接起来完成这个生产线,并使得整体的宽度最小 ,管道只有L型和直线型两种(具体形状见题目),不能旋转管道。每台机器最多有2个输入口和1个输出口。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1e4 + 5;
vector <int> v[N];
int dp[N];

void dfs(int u) {
    int sz = v[u].size();
    if(sz == 0) dp[u] = 1;
    else if(sz == 1) {
        dfs(v[u][0]);
        dp[u] = dp[v[u][0]];
    }
    else {
        int s1 = v[u][0], s2 = v[u][1];
        dfs(s1);
        dfs(s2);
        if(dp[s1] == dp[s2]) dp[u] = dp[s1] + 1;
        else dp[u] = max(dp[s1], dp[s2]);
    }
}

int main() {
    int n, a;
    while(~scanf("%d", &n)) {
        for(int i = 1; i <= n; i++)
            v[i].clear();
        for(int i = 2; i <= n; i++) {
            scanf("%d", &a);
            v[a].push_back(i);
        }
        dfs(1);
        printf("%d\n", dp[1]);
    }
    return 0;
}
时间: 2024-10-11 05:31:55

ZOJ 3805 Machine(简单dp)的相关文章

ZOJ 3805 Machine(树形DP)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=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 mach

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

POJ 3250 Bad Hair Day 简单DP 好题

Description Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads. Each cow i has a sp

hdu1207 汉诺塔II 简单dp

本文出自:http://blog.csdn.net/svitter 题意:汉诺塔,多了一根柱子,问你寻找最快的移动次数. dp [ n ] = dp [ n - j ] * 2 + pow( 2, j ) - 1; 就是把j个汉诺塔移到一根上dp[ n - j ],然后就是普通的汉诺塔问题,即2^n - 1次移动, 然后把剩下的移动过去dp [ n - j ]. 注意pow(2, j )可能超出long long int范围.写二的次方的时候也可用移位算法. #include <iostream

POJ1088:滑雪(简单dp)

题目链接:  http://poj.org/problem?id=1088 题目要求: 一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小.求可以滑落的最长长度. 题目解析: 首先要先排一下序,因为只能高度递减才能滑行.之后就很简单了,就是简单DP. 即:要求的滑坡是一条节点递减并依次相邻的最长路径,可以先根据高度将所有的点进行排序,在i点的时候,遍历0~i-1个点(升序排序,i前面的点的高度一定小于等于i),取相邻点间的大的路径长度 代码如下: #include <iostream

hdu 1087 简单dp

思路和2391一样的.. <span style="font-size:24px;">#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; const int inf=(0x7f7f7f7f); int main() { int a; int s[10005]; int w[10005];

hdu1087 简单DP

I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HD

2014 HDU多校弟九场I题 不会DP也能水出来的简单DP题

听了ZWK大大的思路,就立马1A了 思路是这样的: 算最小GPA的时候,首先每个科目分配到69分(不足的话直接输出GPA 2),然后FOR循环下来使REMAIN POINT减少,每个科目的上限加到100即可 算最大GPA的时候,首先每个科目分配到60分,然后FOR循环下来使REMAIN POINT减少,每个科目的上限加到85即可,如果还有REMAIN POINT,就FOR循环下来加到100上限即可 不会DP 阿 QAQ 过段时间得好好看DP了  =  = 于是默默的把这题标记为<水题集> //

Codeforces 41D Pawn 简单dp

题目链接:点击打开链接 给定n*m 的矩阵 常数k 下面一个n*m的矩阵,每个位置由 0-9的一个整数表示 问: 从最后一行开始向上走到第一行使得路径上的和 % (k+1) == 0 每个格子只能向或走一步 求:最大的路径和 最后一行的哪个位置作为起点 从下到上的路径 思路: 简单dp #include <cstdio> #include <algorithm> #include<iostream> #include<string.h> #include &