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

This problem contains massive input and output, please use efficient IO methods.


Author: ZHU, Jiale; GONG, Yuan

Source: ZOJ Monthly, August 2014

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=50100;

struct Edge
{
    int to,next;
}edge[maxn*2];

int Adj[maxn],Size;
int n;

void init()
{
    Size=0;
    memset(Adj,-1,sizeof(Adj));
}

void Add_Edge(int u,int v)
{
    edge[Size].to=v;
    edge[Size].next=Adj[u];
    Adj[u]=Size++;
}

int dfs(int u,int fa)
{
	int son=0,s1=0,s2=0;
	for(int i=Adj[u];~i;i=edge[i].next)
	{
		int v=edge[i].to;
		if(v==fa) continue;
		if(son==0)
		{
			s1=dfs(v,u);
		}
		else
		{
			s2=dfs(v,u);
		}
		son++;
	}
	if(son==0) return 1;
	else if(son==1) return s1;
	else return min(max(s1+1,s2),max(s1,s2+1));
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        init();
        for(int i=2;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            Add_Edge(x,i);
            Add_Edge(i,x);
        }
		printf("%d\n",dfs(1,1));
    }
    return 0;
}
时间: 2024-10-06 18:02:11

ZOJ 3805 Machine的相关文章

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(简单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

ZOJ 3324 Machine

题意: 一段区间最开始元素都是0  每次操作可以令一段连续区间+1或-1(在加过的前提下才能减)  每次操作输出整段区间有多少段连续的0 思路: 一看区间操作首先考虑线段树  本题n比较大  但是操作数很小  而且每次操作最多影响一段区间(可用两个数字表示区间头尾)  那么就想到了离散化 我和网上题解的离散方式不同  我的更暴力更无脑- -b 为了保证区间连续性  不能只在线段树节点上记录 1 2 4 7 10 等这样类似的离散值  应该这样表示: 1(0-0) 2(1-1) 3(2-2) 4(

ZOJ 3805 (树形DP)

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5337 题目大意:方块连接,呈树形.每个方块有两种接法,一种接在父块边上,宽度+1,一种接在父块上面,宽度+0.且一个母块最多有2个子块.问全局的宽度最小是多少. 解题思路: 对于一个方块,就两种接法. 设dp[i][0]=0,表示接在父块上面. dp[i][1]=1,表示接在父块边上. 对于一个父块,如果没有子块,宽度不变. 如果有一个子块,肯定接在上面,加上子块

ZOJ 1364 Machine Schedule(二分图最大匹配)

题意 机器调度问题 有两个机器A,B A有n种工作模式0...n-1 B有m种工作模式0...m-1 然后又k个任务要做 每个任务可以用A机器的模式i或b机器的模式j来完成 机器开始都处于模式0 每次换模式时都要重启 问完成所有任务机器至少重启多少次 最基础的二分图最大匹配问题 对于每个任务把i和j之间连一条边就可以构成一个二分图 那么每个任务都可以对应一条边 那么现在就是要找最少的点 使这些点能覆盖所有的边 即点覆盖数 又因为二分图的点覆盖数 = 匹配数 那么就是裸的求二分图最大匹配问题了 两

ZOJ 3324 Machine(线段树区间合并)

这道题网上很多代码是错误的,由于后台数据水,他们可以AC. 比如这组数据 10 3 p 0 9 r 0 5 r 6 9 输出应该是 0 1 1 所以有的人直接记录该区间是否被覆盖过的方法是错误的 正确方法应该是记录这段区间的最小高度(就是最接近初始位置的高度),和最小高度对应的最长左区间和右区间 开一个sum记录这段区间最小高度的块数,min_v 记录该区间最小高度 cover作为懒惰标记下推该区间的子区间需要被压几次 KinderRiven C Accepted 6776 170 C++ (g

zoj 3325 Machine(线段树)

题意:0~n-1的数组,初始值为0:执行m个操作,每次操作执行后输出当前值为0的连续段的段数. 操作1: p i j : i~j区间的每个元素值减1 操作2: r i j :i~j区间的每个元素值加1,每个r操作之前,一定有个相应的p操作 数据范围:1 <= n <= 108, 0 <= m <= 20000 线段树现在已经成了竞赛选手的基本功,但是我这块好弱,写下这个题解,方便自己以后复习. 分析,这很明显是个用线段树来解的题,但是数据范围太大.注意到操作次数最多只有20000次

ZOJ 3407 Doraemon&#39;s Cake Machine [数学]

题意: 最多有2000组测试样例,每组样例代表n,m; n代表要把蛋糕平分的份数,m代表必须进行多少次操作. 一共有三种操作 1.竖切   经过蛋糕圆心,将蛋糕整个向下切. 2.横切   平行于蛋糕平面进行平切. 3.复制某块小蛋糕    这种操作只能在1和2所有操作都进行完才能进行. 求: 最少进行多少次复制操作可以将蛋糕分为一样的恰好n种.注意需要用恰好m次操作. 如果没有可行解输出-1. 思路: 假设进行三种操作的数目分别是xyz.然后连立两个式子把z消掉,get 2x(y+1)-(x+y

ZOJ 1364 POJ 1325 -Machine Schedule

Time Limit:1000MS    Memory Limit:10000K Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduling problems differ widely in the nature of the constraints t