A. 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 ≤ 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.

Sample test(s)
input
5
-1
1
2
1
-1
output
3

思路:求树的高度。用并查集,不要压缩路径。

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int father[], cnt;
int max(int x, int y)
{
return x > y ? x : y;
}
void init(int n)
{
for(int i = ;i <= n;i ++)
father[i] = i;
} void find(int x)
{
if(x == father[x])
return;
cnt ++;
find(father[x]);
} void unit(int x, int y)
{
father[x] = y;
return ;
} int main(int argc, char const *argv[])
{
int n, ans, temp;
while(~scanf("%d", &n))
{
init(n);
for(int i = ;i <= n;i ++)
{
scanf("%d", &temp);
if(temp != -)
unit(i, temp);
}
ans = ;
for(int i = ;i <= n;i ++)
{
cnt = ;
find(i);
ans = max(ans, cnt);
}
printf("%d\n", ans+);
}
return ;
}

codeforces --- 115A的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. java web 中的转发和重定向

    假设应用程序的 contextPath 为 /ctx,在 http://localhost:8080/ctx/a/b 资源中,我们转发和重定向到 http://localhost:8080/ctx/x ...

  2. 自己做的demo--左连接

    下面四张表是数据库中已经有的数据: 第一步: 1.left join左连接,left outer join 左外连接,只是写法不同,相同的概念. 2.左连接查出来的结果是一定包含left关键字左边的表 ...

  3. 对java框架的几点认识

    java框架实在是太多了,网上一抄一大段,根本就了解不到什么.我还是以我的经验来说一下j2ee的框架.1.首先力推struts2框架,这是最经典的框架(可以说没有“之一”).可以帮你快速搭建出一个MV ...

  4. 第二部分 Nhibernate中的类型

    NHibernate类型..net类型.数据库字段类型映射关系 因为NHibernate类型和c#数据类型是对应的,所以也分为值类型和引用类型,另外还有几个特殊的类,我们分别介绍: -- 值类型 | ...

  5. 日志记录组件[Log4net]详细介绍

    转载:http://www.cnblogs.com/liwei6797/archive/2007/04/27/729679.html 因为工作中有要用到Log记录,找到一篇不错的文章,就转了过来. 一 ...

  6. NUnit + VS2010 简单入门

    一.环境准备 1. NUnit 2.6.3 下载地址:https://launchpadlibrarian.net/153448476/NUnit-2.6.3.msi 2. VS2010 二.安装 N ...

  7. Sublime Text 3配置LiveReload实现实时刷新

    今天看到一款很强大的插件,LiveReload,实时刷新,也就是说写完html/css/js等不用再到浏览器里按F5啦,在Ctrl+S时浏览器会自动刷新,是不是想想都很爽... Chrome:(据说支 ...

  8. 初学者自学笔记-this的用法

    请注意:这是自学者的笔记,只是个人理解,并非技术分享,如有错误请指正. "this"的意思,简单而言,就是"这个",也就是"当前".谁调用它 ...

  9. 寒假的ACM训练(一)

    今天开始ACM训练,选择了刘汝佳的<挑战编程>,暂时算是开始了. 测评的网址: http://www.programming-challenges.com 第一个题目是水题啦.3n+1. ...

  10. Hadoop学习第一天

    1.hadoop量大,数目多. 存储:分布式,集群的概念,管理(主节点.从节点),HDFS. 分析:分布式.并行.离线计算框架,管理(主节点.从节点),MapReduce. 来源:GFS->HD ...