Codeforces Beta Round #87 (Div. 2 Only)-Party(DFS找树的深度)
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
思路:可以把他们的关系看成一棵树,去寻找树的最大深度,就可以用DSF来找
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define rep(i,n) for(int i=1;i<=N;i++)
using namespace std;
int a[2005];
int pre[2005];
int sum;
int DFS(int x)
{
if(pre[x]==x)
{
return x;
}
else
{
sum++;
// cout<<pre[x]<<endl;
return DFS(pre[x]);
}
}
int main()
{
int N;
cin>>N;
int i;
rep(i,N)
scanf("%d",&a[i]);
rep(i,N)
pre[i]=i;
rep(i,N)
{
if(a[i]!=-1)
pre[i]=a[i];
else
{
pre[i]=i;
}
}
int ans=1;
rep(i,N)
{
sum=1;
DFS(i);
ans=max(ans,sum);
}
cout<<ans<<endl;
return 0;
}
Codeforces Beta Round #87 (Div. 2 Only)-Party(DFS找树的深度)的更多相关文章
- Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树+二分
B. Queue Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/91/B Descrip ...
- Codeforces Beta Round #94 div 2 C Statues dfs或者bfs
C. Statues time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- Codeforces Beta Round #95 (Div. 2) D. Subway dfs+bfs
D. Subway A subway scheme, classic for all Berland cities is represented by a set of n stations conn ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
随机推荐
- #pragma execution_character_set("utf-8")
VC2010增加了“#pragma execution_character_set("utf-8")”,指示char的执行字符集是UTF-8编码. VS2010 设置 字符编码: ...
- maven手动安装oracle驱动到仓库
1. 2.打开http://maven.jahia.org/maven2/一步步打开找到 我需要的版本 https://devtools.jahia.com/nexus/content/groups/ ...
- js实现鼠标拖拽
主要原理: 1.当鼠标按下时,记录鼠标坐标,用到的是 onmousedown: 2.当鼠标移动时,计算鼠标移动的坐标之差,用到的是 onmousemove: 3.当鼠标松开时,清除事件,用到的是 on ...
- while 用法 for 循环的总结
格式化输出.%s %d # name = input('请输入名字:') # age = input('请输入年龄:') # sex = input('请输入性别:') # # msg = '我的名字 ...
- 【总结整理】openlayer加载搜狗地图,qq地图,mapabc
qq http://www.cnblogs.com/gisvip/archive/2012/11/01/2750493.html mapabc http://www.cnblogs.com ...
- Tensorflow学习—— AdamOptimizer
import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data #载入数据集mnist = inpu ...
- (转)typedef和#define的用法与区别
typedef和#define的用法与区别 一.typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译过程的一部分,但它并不实际分配内存空间,实例像: ...
- python3-字典中包含字典
# Auther: Aaron Fan #定义字典及内容av_catalog = { "欧美":{ "www.youporn.com": ["很多免费 ...
- Bootstrap 组件之 Panel
一.简介 Panel 指面板.这里 有例子. 一个典型的面板的代码结构是这样的: .panel.panel-default .panel-heading .panel-title Title Text ...
- 《Head First Servlets & JSP》-6-会话管理-listener etc. demo
工程结构 上下文参数示例 示例程序展示了如何从上下文读取参数,并在上下文监听器中生成属性对象和在上下文中设置属性. 建立一个简单的JavaBean对象作为属性:Dog.java package com ...