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. 添砖加瓦:简述ELK部署

    1.准备工作 ELK下载:https://www.elastic.co/downloads/ jdk version:1.8.0_162 2.环境搭建 ElasticSearch: (1)不能使用ro ...

  2. 当鼠标hover的时候,使用tip将overflow:hidden隐藏的文字显示完全

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. 关于Linux文件系统

    前言 文件系统是在内核中实现,能够对存储在磁盘上的二进制数据进行有效的层次化管理的一种软件.而用户程序为了实现在磁盘上使用或者创建文件,向内核发起系统调用(实际由文件系统向内核发起的系统调用)并转换为 ...

  4. hexo+github搭建自己的博客

    之前很早就想用hexo弄一个自己独立的博客了,在博客园也写了很多的博客,不过不喜欢博客园的风格.不过今天,终于折腾成功了,用hexo搭建了一个在github写的博客,开心,后面会将自己以前的博客慢慢迁 ...

  5. diary20180428

    17:05:59 今天早晨去了图书馆.学习了一把vscode.试图在河边看电脑,总有小虫不让我专心. 23:27:34 看纯黑直播打战神,有点感触. 动漫或游戏,角色觉醒,实力大增,小时候(甚至现在) ...

  6. Python——1变量和数据类型(内含其他知识点链接)

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

  7. Python Type Hint类型注解

    原文地址:https://realpython.com/python-type-checking/ 在本指南中,你将了解Python类型检查.传统上,Python解释器以灵活但隐式的方式处理类型.Py ...

  8. tfgan折腾笔记(二):核心函数详述——gan_model族

    定义model的函数有: 1.gan_model 函数原型: def gan_model( # Lambdas defining models. generator_fn, discriminator ...

  9. 使用Blazor Server 线路处理程序 (circuit handler)跟踪打开的SignalR连接

    Blazor服务器允许定义线路处理程序(circuit handler)代码,该处理程序(handler)允许在更改用户线路状态时运行此代码. 线路处理程序(circuit handler)是通过从C ...

  10. 线程(Thread)的四种停止方式

    1.正常的程序启动,停止 2.使用退出标记,一般程序在run()方法后,线程会正常结束.但是有一些伺服线程还在运行,他们运行时间较长,只有当外部条件满足时,他们才会停止.实现如下: public cl ...