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只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- 30、ADO.NET、事务、DataSet
ADO.NET ADO.NET是一组用于和数据源进行交互的面向对象类库.通常数据源是数据库,但也可以是文本文件.Excel表格.XML文件. 说白了就是使用.net操作数据库的一套类库. ADO.NE ...
- 单点登录SSO原理
最近接触了一点单点登录的知识,有一点理解,记录一下.有些问题并没有找到完美的解决方法,还需要找点已有框架来看看. 欢迎留言探讨. 1 概念 1.1 概念及理解 有一个网上广为流传的 ...
- json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不 ...
- Perl连接Sqlite数据库
Sqlite是一个小巧的嵌入式关系型数据库,几乎可以嵌入所有编程语言,特别是C,C++,PHP,Perl等.这里就介绍如何用Perl连接并操作Sqlite数据库. use DBI; # perl用以操 ...
- 【原创】The Error in Android developing
本文停止更新.... 鼠标渣渣 ,点一次 成两次 R.java文件不幸中枪 被拖动修改... 最后application编译运行时 解决方案:project-clean 问题解决了.. 2 ...
- 为什么我们需要使用 touch 命令
为什么我们需要使用 touch 命令 既然 touch 命令描述的是改变时间戳,那么我们可能会想为什么我们需要去改变时间戳呢?这个问题会引发我们的深思.然而,我想有个理由可以解释为什么我们需要使用它. ...
- 基础-函数3(IIFE立即执行函数)
参考链接: http://benalman.com/news/2010/11/immediately-invoked-function-expression/#iife http://segmentf ...
- 1、Python django 框架下的word Excel TXT Image 等文件的上传
1.文件上传(input标签) (1)html代码(form表单用post方法提交) <input class="btn btn-primary col-md-1" styl ...
- 关闭linux终端命令行退格报警声(centos7亲测有效)
首先这个声音不是外置音频设备发出,而是主板上的蜂鸣器 1,使用root账户登录 2,vi 打开 ~/.bashrc 脚本 3,在脚本的最后一行加上 setterm -blength 0 4,保存脚本 ...
- opencart 百度登录和百度钱包支付插件 响应式适应pc/mobile
OpenCart(http://www.opencart.com/,http://www.opencartchina.com/)是国外著名的开源电子商务系统, 优势在于前台界面的设计非常适合欧美购物者 ...