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> ...
随机推荐
- from xml.etree import cElementTree as ET
- IE的haslayout
haslayout 是Windows Internet Explorer渲染引擎的一个内部组成部分.在InternetExplorer中,一个元素要么自己对自身的内容进行计算大小和组织,要么依赖于父元 ...
- 思考题-关于CSS(转)
dl, dt, dd三个标签浏览器默认margin值多少?是否有标签默认文字粗体? line-height:150%和line-height:1.5的区别是? float为何会让外部容器高度塌陷?这是 ...
- css知多少(5)——选择器(转)
css知多少(5)——选择器 1. 引言 从本节开始,就进入本系列的第二个部分——css和html的结合——说白了就是选择器. CSS中定义了样式,如何将这些样式设置到相应的html节点上?就不得 ...
- Opencv Laplacian(拉普拉斯算子)
#include <iostream>#include <opencv2/opencv.hpp>#include <math.h> using namespace ...
- Gearman 分布式的异步任务分发框架
What is Gearman? Gearman provides a generic application framework to farm out work to other machines ...
- linux 开启终端256色支持
一.简介 一般的Linux发行版默认的终端都是16色的,但事实上几乎所有的终端都支持256色终端.本文介绍开启终端256色支持的方法. 二.操作步骤 1)检查终端是否支持256色 http://www ...
- python3-函数的参数的四种简单用法:
def print_two(*args): arg1, arg2 = args print "arg1: %r, arg2: %r" % (arg1,arg2) ...
- Git发布本地项目至仓库命令行操作流程
1.初始化项目 git init 2.创建名称为 gh-pages 新分支(若直接发布至master分支,忽略此步) git checkout --orphan gh-pages 3.把所有内容加入本 ...
- 具有增删改查功能的表格Demo--【BootStrap】
http://blog.csdn.net/wangmei4968/article/details/48437175