Party

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 ≤ pi ≤ n 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 (pi ≠ i). 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

题意:给出n个点和他们的父结点,现在要将他们分成一些小组,小组内不能出现任何一个人的祖先,问最少可以分成几个组
思路:在一个组内不能出现一个点的祖先,也不能出现一个点的后代,因为如果出现了一个点的后代,则这个结点就是那些后代的祖先,这是不合法的
所以我们可以把深度相同的结点分位一组,最大深度就是要分的组数,因为题目的上下级关系可能会分成很多树形成一个森林,所以我们把每个结点当作树根来统计深度,最后保存一个最大的深度作为答案输出

 
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
typedef long long ll;
const int amn=1e5+;
int n,ans=,m[amn],deep[amn],cnt;
vector<int> eg[amn];
queue<int> q;
void bfs(int rt){
while(q.size())q.pop();q.push(rt);
memset(deep,,sizeof deep);
deep[rt]=;
cnt=;
while(q.size()){
int u=q.front();q.pop();
cnt=max(cnt,deep[u]);
for(int i=;i<eg[u].size();i++){
int v=eg[u][i];
deep[v]=deep[u]+;
q.push(v);
}
}
ans=max(ans,cnt);
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&m[i]);
if(m[i]!=-){
eg[m[i]].push_back(i);
}
}
ans=;
for(int i=;i<=n;i++){
bfs(i);
}
printf("%d\n",ans);
}
/**
题意:给出n个点和他们的父结点,现在要将他们分成一些小组,小组内不能出现任何一个人的祖先,问最少可以分成几个组
思路:在一个组内不能出现一个点的祖先,也不能出现一个点的后代,因为如果出现了一个点的后代,则这个结点就是那些后代的祖先,这是不合法的
所以我们可以把深度相同的结点分位一组,最大深度就是要分的组数,因为题目的上下级关系可能会分成很多树形成一个森林,所以我们把每个结点当作树根来统计深度,最后保存一个最大的深度作为答案输出
**/

[树的深度] Party的更多相关文章

  1. 27.二元树的深度[BinaryTreeDepth]

    [题目] 输入一棵二元树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 例如 10                          ...

  2. xgboost/gbdt在调参时为什么树的深度很少就能达到很高的精度?

    问题: 用xgboost/gbdt在在调参的时候把树的最大深度调成6就有很高的精度了.但是用DecisionTree/RandomForest的时候需要把树的深度调到15或更高.用RandomFore ...

  3. 小小c#算法题 - 10 - 求树的深度

    树型结构是一类重要的非线性数据结构,树是以分支关系定义的层次结构,是n(n>=0)个结点的有限集.关于树的基本概念不再作过多陈述,相信大家都有了解,如有遗忘,可翻书或去其他网页浏览以温习. 树中 ...

  4. AlphaGo原理-蒙特卡罗树搜索+深度学习

    蒙特卡罗树搜索+深度学习 -- AlphaGo原版论文阅读笔记     目录(?)[+]   原版论文是<Mastering the game of Go with deep neural ne ...

  5. 【C++竞赛 D】树的深度

    时间限制:1s 内存限制:32MB 问题描述 数据结构中定义,树的高度为一棵树中所有节点的层次的最大值.现在yyy有一棵树请你帮他求出该树的高度. 输入描述 第一行一个整数T(1≤T≤20)表示数据组 ...

  6. 数据结构5_java---二叉树,树的建立,树的先序、中序、后序遍历(递归和非递归算法),层次遍历(广度优先遍历),深度优先遍历,树的深度(递归算法)

    1.二叉树的建立 首先,定义数组存储树的data,然后使用list集合将所有的二叉树结点都包含进去,最后给每个父亲结点赋予左右孩子. 需要注意的是:最后一个父亲结点需要单独处理 public stat ...

  7. PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  8. 剑指offer38:输入一棵二叉树,求该树的深度

    1 题目描述 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 2 思路和方法 深度优先搜索,每次得到左右子树当前最大路径,选择 ...

  9. STA树的深度(树型DP)

    STA树的深度 题目大意 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面N-1条边. Outpu ...

  10. 树的深度———树形DP

    题目描述 输入 输出 样例 样例输入 样例输出 7 分析 这道题数据有1000000,把每一个顶点都枚举一次显然不现实,肯定会T掉 所以,我们还是从图中找规律 按照习惯,我们先把1号节点作为根节点模拟 ...

随机推荐

  1. 添砖加瓦:Linux /proc目录简介

    Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文件系统的方式为访问系 ...

  2. 在CodaLab上提交MURA竞赛的结果

    What is MURA? MURA (musculoskeletal radiographs) is a large dataset of bone X-rays. Algorithms are t ...

  3. loadrunner通过web的post请求方法测接口

    loadrunner通过web的post请求方法测接口 loginapi() 模拟APP发送请求给Cloud, Action() "Name=input","Value= ...

  4. MAC使用nginx分发80至8080端口

    由于项目必须要启动80端口,但是mac系统中非root用户无法直接使用1024以下的端口 2.释放apache的80端口 由于Mac OS是自带Apache服务的,它本身占用了80端口,首先你需要将A ...

  5. IDEA 运行junit单元测试方法

    配置Run,增加Junit 最终配置如下:

  6. C++走向远洋——61(项目一、排序函数模板)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  7. [红日安全]Web安全Day5 - 任意文件上传实战攻防

    本文由红日安全成员: MisakiKata 编写,如有不当,还望斧正. 大家好,我们是红日安全-Web安全攻防小组.此项目是关于Web安全的系列文章分享,还包含一个HTB靶场供大家练习,我们给这个项目 ...

  8. Python 安全修改私有属性

    设置私有属性之后,如何修改私有属性 class Room: def __init__(self,name,length,width): self.__name = name self.__length ...

  9. 可视化工作流程设计开发OA系统,一两个程序员就搞定!

    随着信息化的发展,越来越多的公司老板要求实现企业审批流程化.一个公司在初期,人员少,流程简单,员工也会经常不按工作流程来走,甚至有些跨部门的工作因为关系原因,没有走工作流程就实施,导致后期出现问题或者 ...

  10. day05基本运算符,格式化输出,垃圾回收机制

    内容大纲:1.垃圾回收机制详解(了解) 引用计数 标记清除 分代回收 2.与用户交互 接收用户输入 # python3中 input # python2.7(了解) input raw_input 格 ...