HNU-OJ 2787. Party

time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true:

  • Employee A is the immediate manager of employee B
  • Employee B has an immediate manager employee C such that employee A is the superior of employee C.

The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager.

Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B.

What is the minimum number of groups that must be formed?

Input

The first line contains integer n (1≤n≤2000) − the number of employees.

The next n lines contain the integers pi (1≤pin or pi=-1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager.

It is guaranteed, that no employee will be the immediate manager of him/herself (pii). Also, there will be no managerial cycles.

Output

Print a single integer denoting the minimum number of groups that will be formed in the party.

Examples

Input

5
-1
1
2
1
-1

Output

3

Note

For the first example, three groups are sufficient, for example:

  • Employee 1
  • Employees 2 and 4
  • Employees 3 and 5

思路

建树,求树的高度。

时间复杂度为O(n^2).

#include<iostream>
using namespace std;
int a[2001];
int height=0;
void tre(int father, int h){//father-结点在数组中的序号,h-积累的高度 
	if (a[father]==-1)//为树的根节点 不能用! 
	{
		if (h>height)
		height=h;
		return;
	}
	else{
		h++;
		tre(a[father]-1,h);//递归建树 
	}
}
int main(){
	int n;
	cin>>n;
	for (int i=0; i<n; i++){
		cin>>a[i];
	}
	for (int i=0; i<n; i++){
		tre(i,1);
	}
	cout<<height<<endl;
	return 0;
}

### 回答1: 申请内存、释放内存和查看内存使用情况。这些操作将通过系统调用来实现。内存管理器的主要功能是管理系统中的内存分配和释放,确保内存使用的高效性和安全性。虽然内存管理器还没有完成,但开发人员正在努力工作,以便在不久的将来发布完整的版本。 ### 回答2: 内存分配、内存释放和内存查找。内存分配可以让程序在运行时获取需要的内存空间,内存释放则是将不再需要的内存空间还给操作系统,以便重新分配给其他程序使用。内存查找则是检查某个内存地址是否已经被分配。这些功能对于操作系统运行和程序开发都具有很大的重要性。 内存管理器的实现主要考虑了几个因素。首先是内存的物理组织形式。操作系统需要能够识别内存的物理组织形式,才能够有效地进行内存管理。其次是内存的分配和释放。操作系统需要能够根据不同的需求分配不同大小的内存空间,并且能够及时地释放内存空间以提高系统的运行效率。还有一个重要的因素是内存的安全性。操作系统需要确保不同程序之间的内存空间互相隔离,避免出现内存泄露或者内存破坏等问题。 对于内存管理器的实现方式,目前有很多种不同的方法。其中,最常见的是分页内存管理方式。这种方式将内存空间分成若干个大小相等的页面,每个页面都有自己的地址和内存状态,操作系统只需要记录每个页面的状态和分配情况即可。当程序需要内存空间时,内存管理器会寻找空闲的页面,并将其分配给程序使用。当程序不再需要这些内存空间时,内存管理器会将其释放,并将页面标记为未分配状态。 总之,内存管理器是一个操作系统非常重要的组件,对于系统的性能和稳定性都有很大的影响。hnu-os的内存管理器的实现将进一步完善,以满足更多的需求。 ### 回答3: 内存分配、内存释放和内存查看。但是,由于内存管理器的实现需要许多复杂的算法和微妙的技巧,因此开发人员需要花费更多的时间来完成它。 内存管理器是操作系统中最重要的组件之一,因为它确保操作系统能够正确地使用计算机的内存。内存管理器的主要任务是将物理内存地址映射到虚拟内存地址,并跟踪哪些内存块已经被分配、哪些是空闲的,以及哪些正在使用。 在操作系统中,内存被分割成多个块。当一个程序需要内存时,内存管理器会分配一个或多个块来满足程序的要求。当程序完成它的工作并将内存释放时,内存管理器会将这些块标记为空闲状态,以备后续程序再次使用。 内存管理器还需要处理许多其他事项,例如内存碎片化和内存保护。内存碎片化是指当程序释放一些内存块时,内存管理器会留下一些无法使用的小空间,这些空间可能过小,无法再分配给其他程序。内存保护是指内存管理器可以防止程序访问不属于它的内存区域,从而保护操作系统和其他程序的稳定性和安全性。 总之,内存管理器对于计算机系统的稳定性和性能至关重要。虽然它可能需要一些时间来完成,但一旦实现,它将使操作系统功能更加完整且能够更好地支持更复杂的任务和应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值