Codeforces 115A- Party(DFS)
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 <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
int n,a[2010],ans,tem;
void dfs(int u)
{
tem++;
if(a[u]==-1)
return ;
dfs(a[u]);
}
void solve()
{
ans=-INF;
for(int i=1;i<=n;i++)
{
tem=0;
dfs(i);
ans=max(ans,tem);
}
printf("%d\n",ans);
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
scanf("%d",a+i);
solve();
}
return 0;
}
Codeforces 115A- Party(DFS)的更多相关文章
- Codeforces 116C - Party(dfs)
n个人,每个人之多有一个上司.“上司”关系具有传递性.求最少将人分成多少组,每组中的每个人的上司或者间接上司都不在该组.拿到题就用树的直径wa了一炮... 正解是有向无环森林的最长路.从每个跟节点df ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的深度优先搜索遍历(DFS)
关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 搜索——深度优先搜索(DFS)
设想我们现在身处一个巨大的迷宫中,我们只能自己想办法走出去,下面是一种看上去很盲目但实际上会很有效的方法. 以当前所在位置为起点,沿着一条路向前走,当碰到岔道口时,选择其中一个岔路前进.如果选择的这个 ...
随机推荐
- LocalDateTime查找最近的五分钟点
/** * 最近的五分钟 * @param dateTime * @return */ public static LocalDateTime getNear5(LocalDateTime dateT ...
- go的接口
一.接口定义 接口类型 在讲基础数据类型时,我们曾提了一下 interface 数据类型,这个数据类型就是接口类型 什么是接口 Go 语言不是"传统"的面向对象的编程语言:它里面没 ...
- [BZOJ2321,LuoguP1861]星(之)器
丧心病狂的神仙题 丧心病狂的神仙题 丧心病狂的神仙题 显然,不管你怎么移动,答案都是一定的 然后我们很快能联系到物理里面的能量守恒,于是自然地我们要给每个点搞一个势能出来 然后把势能的表达式写出来就可 ...
- 省市区县的sql语句——区县
DROP TABLE IF EXISTS `area`; CREATE TABLE `area` ( `id` int(11) NOT NULL AUTO_INCREMENT, `code` va ...
- Microsoft SQL Server 2005技术内幕:T-SQL查询笔记
logical operation:基于微软查询处理概念模型的逻辑操作.例如,联接运算符的physical operation属性表示联接算法(nested loops,merge ,hash)物理运 ...
- C#获取窗口大小和位置坐标 GetWindowRect用法
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetWi ...
- jQuery顺序加载图片(初版)
浏览器加载图片区别: IE:同时加载与渲染 其他:加载完之后再渲染 根据这个差异用jQuery做个实例:按顺序加载一组图片,加载完成后提示. <!DOCTYPE html> <htm ...
- windows 实时性
在硬件编程时,大部分非智能硬件并没有主动通知反馈功能,需要PC主动轮询状态寄存器去查询硬件状态.对于运动类控制器,查询的时机(间隔)在一定程度上影响着准确率与系统负载.即使不考虑系统负载,在1000H ...
- sql server 查询数据判断为空
and xxx is NOT null and xxx is null
- Python总结1
时间:24日下午输入:input()输出:print()格式化输出通过某种占位符用于替换字符串中某个位置的字符.占位符:%s:可以替换任意类型%d:可以替换数字类型 需要掌握的#1.strip去左右两 ...