CF115A.Party

入门

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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?

一家公司有 nn 名员工,编号从 11 到 nn。每名员工要么没有直接上级(即无直属经理),要么恰好有一名直接上级,且该直接上级是另一名编号不同的员工。若满足以下任一条件,则称员工 AA 是员工 BB 的上级:

  • 员工 AA 是员工 BB 的直接上级;
  • 员工 BB 有一名直接上级 CC,且员工 AA 是员工 CC 的上级。

公司中不会出现管理循环,即不存在某名员工,使其成为其自身直接上级的上级。

今天公司将举办一场聚会,需要将全部 nn 名员工划分为若干组:每名员工必须且仅属于一个组;此外,在任意一个组内,不能存在两名员工 AA 和 BB,使得 AA 是 BB 的上级。

问:至少需要划分成多少个组?

输入格式

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

The next n lines contain the integers p__i (1 ≤ p__i ≤ n or p__i = -1). Every p__i denotes the immediate manager for the i-th employee. If p__i 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 (p__i ≠ i). Also, there will be no managerial cycles.

第一行包含一个整数 nn(1≤n≤20001 \leq n \leq 2000)—— 员工人数。

接下来的 nn 行每行包含一个整数 pip_i(1≤pi≤n1 \leq p_i \leq n 或 pi=−1p_i = -1)。每个 pip_i 表示第 ii 位员工的直接上级。若 pi=−1p_i = -1,则表示第 ii 位员工没有直接上级。

保证不会出现某位员工是其自身直接上级的情况(即 pi≠ip_i \neq i),且不存在管理循环。

输出格式

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

输出一个整数,表示聚会上形成的最少组数。

输入输出样例

  • 输入#1

    5
    -1
    1
    2
    1
    -1

    输出#1

    3

说明/提示

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

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

对于第一个例子,三个组就足够了,例如:

  • 员工 1
  • 员工 2 和 4
  • 员工 3 和 5

输入解题思路,AI测评打分。不知道怎么写?

首页