2019-07-29

09:01:06

A 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 nemployees 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

方法一:

#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int father[maxn]; int find_step(int x)
{
int dep = ;
while(father[x] != x){
x = father[x];
dep++;
}
return dep;
} int main()
{
int n,x;
scanf("%d",&n);
for(int i = ;i <= n;i++){
scanf("%d",&x);
if(x == -)
father[i] = i;
else
father[i] = x;
}
int sum = ;
for(int i = ;i <= n;i++){
sum = max(sum,find_step(i));
}
// printf("\n%d\n",ans + 1);
cout << sum + ;
return ;
}

方法二:

#include<bits/stdc++.h>
using namespace std;
vector<int>g[];
int deep,maxn;
int visit[];
int a[]; void dfs(int x); int main()
{
int n;
cin >> n;
for(int i = ; i <= n; i++)
{
cin >> a[i];
if(a[i] == -) continue;
g[a[i]].push_back(i);
}
deep = ;
maxn = ;
memset(visit,,sizeof(visit));
for(int i = ; i <= n; i++)
{
if(a[i] == - && visit[i] == ) //以-1作为树根,深度遍历
{
visit[i] = ;
dfs(i);
}
}
cout << maxn << endl;
return ;
} void dfs(int x)
{
for(int i = ; i < g[x].size(); i++)
{
int u = g[x][i];
if(visit[u] == )
{
visit[u] = ;
deep++;
dfs(u);
if(deep > maxn) maxn = deep;
deep--;
}
}
}

方法三:

#include<bits/stdc++.h>
using namespace std;
int f[];
int main()
{
int n;
int temp = ;
int ant = ;
cin >> n;
for(int i = ; i <= n; i++)
{
cin >> f[i];
}
for(int i = ; i <= n; i++)
{
temp = ;
for(int j = i; j != -; j = f[j])
{
temp++;
}
ant = max(temp, ant);
}
cout << ant;
return ;
}
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2010;
int father[maxn]; int find_step(int x)
{
int dep = 0;
while(father[x] != x){
x = father[x];
dep++;
}
return dep;
} int main()
{
int n,x;
scanf("%d",&n);
for(int i = 1;i <= n;i++){
scanf("%d",&x);
if(x == -1)
father[i] = i;
else
father[i] = x;
}
int sum = 0;
for(int i = 1;i <= n;i++){
sum = max(sum,find_step(i));
}
// printf("\n%d\n",ans + 1);
cout << sum + 1;
return 0;
}
 

DFS集训的更多相关文章

  1. ACM集训的Day3 B。。。盲目搜索之DFS。。。

    milk 一.题目描述: gzp有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数, 最初,A和B桶都是空的,而C桶是装满牛奶的.有时,农民把牛奶从一个桶倒到 另一个桶中,直到被灌 ...

  2. 【NOI2019集训题2】 序列 后缀树+splay+dfs序

    题目大意:给你一个长度为$n$的序列$a_i$,还有一个数字$m$,有$q$次询问 每次给出一个$d$和$k$,问你对所有的$a_i$都在模$m$意义下加了$d$后,第$k$小的后缀的起点编号. 数据 ...

  3. QDEZ集训笔记【更新中】

    这是一个绝妙的比喻,如果青岛二中的台阶上每级站一只平度一中的猫,差不多站满了吧 自己的理解 [2016-12-31] [主席树] http://www.cnblogs.com/candy99/p/61 ...

  4. 【62测试】【状压dp】【dfs序】【线段树】

    第一题: 给出一个长度不超过100只包含'B'和'R'的字符串,将其无限重复下去. 比如,BBRB则会形成 BBRBBBRBBBRB 现在给出一个区间[l,r]询问该区间内有多少个字符'B'(区间下标 ...

  5. nyoj325 zb的生日(DFS)

    zb的生日 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么 ...

  6. 2015UESTC 暑假集训总结

    day1: 考微观经济学去了…… day2: 一开始就看了看一道题目最短的B题,拍了半小时交了上去wa了 感觉自己一定是自己想错了,于是去拍大家都过的A题,十分钟拍完交上去就A了 然后B题写了一发暴力 ...

  7. 2013ACM暑假集训总结-致将走上大三征途的我

    回想起这个暑假,从开始与雄鹰一起的纠结要不要进集训队,与吉吉博博组队参加地大邀请赛,害怕进不了集训队.当时激励我月份开始接触的,记得当时在弄运动会来着,然后就问了雄鹰一些输入输出的东西,怀着满心的期待 ...

  8. 分西瓜(DFS)

    描述今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么庆祝生日,经过调查,zb发现C小加和never都很喜欢吃西瓜,而且一吃就是一堆的那种,zb ...

  9. zb的生日(暴搜dfs)

    zb的生日 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么 ...

随机推荐

  1. UFUN函数 UF_ATTR函数(UF_ATTR_assign ,UF_ATTR_read_value )

    UF_initialize(); tag_t ; ]="零件名称"; UF_ATTR_value_t value; value.type=UF_ATTR_string; value ...

  2. 【洛谷P4319】 变化的道路 线段树分治+LCT

    最近学了一下线段树分治,感觉还蛮好用... 如果正常动态维护最大生成树的话用 LCT 就行,但是这里还有时间这一维的限制. 所以,我们就把每条边放到以时间为轴的线段树的节点上,然后写一个可撤销 LCT ...

  3. 【csp模拟赛4】基站建设 (station.cpp)

    [题目描述] 小 Z 的爸爸是一位通信工程师,他所在的通信公司最近接到了一个新的通 信工程建设任务,他们需要在 C 城建设一批新的基站. C 城的城市规划做得非常好,整个城市被规整地划分为 8 行 8 ...

  4. 第12组 Beta测试(5/5)

    Header 队名:To Be Done 组长博客 作业博客 团队项目进行情况 燃尽图(组内共享) 展示Git当日代码/文档签入记录(组内共享) 注: 由于GitHub的免费范围内对多人开发存在较多限 ...

  5. hbase 由于zookeeper问题导致连接失败问题

    问题现象: 使用hbase shell 连接报如下问题: 2019-10-09 10:37:18,855 ERROR [main] zookeeper.RecoverableZooKeeper: Zo ...

  6. assert(0)的作用

    捕捉逻辑错误.可以在程序逻辑必须为真的条件上设置断言.除非发生逻辑错误,否则断言对程序无任何影响.即预防性的错误检查,在认为不可能的执行到的情况下加一句ASSERT(0),如果运行到此,代码逻辑或条件 ...

  7. 第06组 Alpha冲刺(5/6)

    队名:拾光组 组长博客链接 作业博客链接 团队项目情况 燃尽图(组内共享) 组长:宋奕 过去两天完成了哪些任务 主要完成了个人中心模块的接口设计 完善后端的信息处理 GitHub签入记录 接下来的计划 ...

  8. 利用select/poll监听多个设备详解

    如果一个应用程序去处理多个设备,例如应用程序读取网路数据,按键,串口,一般能想到的有三种方法: 方法1:串行+阻塞的方式读取:while(1) { read(标准输入);read(网络);}缺点:每当 ...

  9. IDEA将指定package(指定文件)打成jar包

    写在前面 真的是好记性不如烂笔头 需求 将项目中包名为org的package打成jar包 步骤 1.选择Artifacts>绿色+号>JAR>Empty name自定义, 我这里命名 ...

  10. SQLServer string_split函数,撕裂函数,撕开函数

    declare @name char(1000) --注意:char(10)为10位,要是位数小了会让数据出错 set @name='s{sss}fc{fggh}dghdf{cccs}x' selec ...