首页 > 代码库 > ZOJ 3805 Machine(树形DP)
ZOJ 3805 Machine(树形DP)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3805
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.
依据题意,可以很容易的转换成二叉树。
用 son[id][j] 表示编号为id 的节点的第j个儿子。当 son[id][0]==son[id][1] 时 dp[id]= son[id][0]+1
当 son[id][0]!=son[id][1] 时 dp[id]=max(son[id][0],son[id][1])
#include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<string> #include<algorithm> #include<cmath> #include<vector> #include<set> #include<map> #define MAXN 10010 using namespace std; #define CLR(A) memset(A,0,sizeof(A)) vector<int>son[MAXN]; bool vis[MAXN]; int N,dp[MAXN],ans; int work(int id){ int sz=son[id].size(); if(sz==0){ dp[id]=1; return dp[id]; } for(int i=0;i<sz;i++){ int v=son[id][i]; if(vis[v]) continue; vis[v]=1; work(v); } if(sz==1){ dp[id]=dp[son[id][0]]; return dp[id]; } if(sz==2){ int u=son[id][0],v=son[id][1]; int t1=dp[u],t2=dp[v]; if(t1==t2) dp[id]=dp[u]+1; else dp[id]=max(t1,t2); return dp[id]; } } int main(){ while(~scanf("%d",&N)){ int x; for(int i=0;i<=N+1;i++) son[i].clear(); for(int i=2;i<=N;i++){ scanf("%d",&x); son[x].push_back(i); // son[x].push_back(i); } CLR(vis);CLR(dp); vis[1]=1; ans=work(1); printf("%d\n",ans); } return 0; }
ZOJ 3805 Machine(树形DP)