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. 【转】PHP中被忽略的性能优化利器:生成器.md

      PHP  如果是做Python或者其他语言的小伙伴,对于生成器应该不陌生.但很多PHP开发者或许都不知道生成器这个功能,可能是因为生成器是PHP 5.5.0才引入的功能,也可以是生成器作用不是很明 ...

  2. java里面的设计模式

    文章目录 Creational(创建模式) 1. Abstract factory: 2. Builder: 3. Factory: 4. Prototype: 5. Singleton: 6. Ch ...

  3. 安卓权威编程指南-笔记(第24章 Looper Handler 和 HandlerThread)

    AsyncTask是执行后台线程的最简单方式,但它不适用于那些重复且长时间运行的任务. 1. Looper Android中,线程拥有一个消息队列(message queue),使用消息队列的线程叫做 ...

  4. TCP传输连接管理

    TCP传输连接管理 一.传输连接的三个阶段 1.1.概述 传输连接就有三个阶段,即:连接建立.数据传送和连接释放. 连接建立过程中要解决以下三个问题: 要使每一方能够确知对方的存在. 要允许双方协商一 ...

  5. Spring,SpringMVC,MyBatis,SSM配置文件比较

    Spring配置文件: applicationContext.xml applicationContext.xml是Spring的核心配置文件 IOC/DI,AOP相关配置都是在这个文件中 Sprin ...

  6. 7-46 jmu-python-求单词长度 (10 分)

    输入n个单词,计算每个单词长度.对单词长度排序,分行输出单词长度及其单词. 输入格式: 行1:单词个数n 分行输入n个单词 输出格式: 分行输出单词长度及其单词.(单词长度,单词)用元组表示 输入样例 ...

  7. Django中的session的使用

    一.Session 的概念 cookie 是在浏览器端保存键值对数据,而 session 是在服务器端保存键值对数据 session 的使用依赖 cookie:在使用 Session 后,会在 Coo ...

  8. IOS 7 UITableView cell lines不能靠左解决方法

    添加一句:[UITableViewappearance].separatorInset=UIEdgeInsetsZero; 就可以解决啦.

  9. scrapy mid中间件一般处理方法

    import user_agent import requests class UA_midd(object): def process_request(self,request,spider): r ...

  10. 【图文+视频新手也友好】Java一维数组详细讲解(内含练习题答案+详解彩蛋喔~)

    目录 视频讲解: 一.数组的概述 二.一维数组的使用 三.Arrays工具类中的sort方法(sort方法用的多,我们具体讲一下) 四.数组中的常见异常 五.一维数组练习题 六.彩蛋(本期视频使用的P ...