codeforces --- 115A
3 seconds
256 megabytes
standard input
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?
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.
Print a single integer denoting the minimum number of groups that will be formed in the party.
5
-1
1
2
1
-1
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的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- CSS排版页面
创建CSS文件如下: @charset "utf-8"; /* CSS Document */ *{ margin:0px; padding:0px; border:0px; } ...
- C#中隐式类型本地变量var
在新接触的项目中,看到很多声明变量时用var.只记得在javascript中声明变量用var.今天在家里看C#和.Net高级编程,看到隐式变量这一块,就总结一下C# 中隐式变量var的用法. 1.C# ...
- 避免 TCP/IP 端口耗尽
转载:http://www.cnblogs.com/tianzhiliang/archive/2011/06/27/2091214.html 当客户端启动到服务器的 TCP/IP 套接字连接时,客户端 ...
- org.hibernate.HibernateException: No CurrentSessionContext configured!
hibernate可以通过两种方式获得Session: getCurrentSession() 和openSession(). 当通过getCurrentSession()方法时,需要在 hibern ...
- 一个实例明白AutoResetEvent和 ManulResetEvent的用法
先看一段代码: public class WaitHandlerExample { public static AutoResetEvent waitHandler; ...
- 详细介绍Linux shell脚本基础学习
Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提.1. Lin ...
- Oracle RAC OCR 和 VotingDisk 的备份与恢复
一:Voting DiskVoting Disk 这个文件主要用于记录节点成员状态,在出现脑裂时,决定那个Partion获得控制权,其他的Partion必须从集群中剔除.Voting disk使用的是 ...
- 使用java的Calendar对象获得当前日期的上几个度开始、结束时间
思路: 先获得当前季度的开始和结束日期,在当前日期的基础上往前推3个月即上个季度的开始和结束日期 /** * @param flag true:开始日期:false:结束日期 * @return */ ...
- C# 静态类和非静态类的区别
静态类和非静态类的区别 静态类: static 关键字 调用 类名.方法 在静态方法中只能访问静态成员 在静态类中只能有静态成员 在非静态类中 即可有非静态成员,也可以有静态成员 在静态 ...
- macbook pro retina 编程字体推荐
使用VS2010.VS2012.Qt Creator编译工具首推等宽字体,等宽字体中consolas. 首先大家都知道等宽对于编码来说的直观性不言而喻,其次retina屏幕的特殊性,整天用特别小的字体 ...