DFS集训
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
5
-1
1
2
1
-1
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集训的更多相关文章
- ACM集训的Day3 B。。。盲目搜索之DFS。。。
milk 一.题目描述: gzp有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数, 最初,A和B桶都是空的,而C桶是装满牛奶的.有时,农民把牛奶从一个桶倒到 另一个桶中,直到被灌 ...
- 【NOI2019集训题2】 序列 后缀树+splay+dfs序
题目大意:给你一个长度为$n$的序列$a_i$,还有一个数字$m$,有$q$次询问 每次给出一个$d$和$k$,问你对所有的$a_i$都在模$m$意义下加了$d$后,第$k$小的后缀的起点编号. 数据 ...
- QDEZ集训笔记【更新中】
这是一个绝妙的比喻,如果青岛二中的台阶上每级站一只平度一中的猫,差不多站满了吧 自己的理解 [2016-12-31] [主席树] http://www.cnblogs.com/candy99/p/61 ...
- 【62测试】【状压dp】【dfs序】【线段树】
第一题: 给出一个长度不超过100只包含'B'和'R'的字符串,将其无限重复下去. 比如,BBRB则会形成 BBRBBBRBBBRB 现在给出一个区间[l,r]询问该区间内有多少个字符'B'(区间下标 ...
- nyoj325 zb的生日(DFS)
zb的生日 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么 ...
- 2015UESTC 暑假集训总结
day1: 考微观经济学去了…… day2: 一开始就看了看一道题目最短的B题,拍了半小时交了上去wa了 感觉自己一定是自己想错了,于是去拍大家都过的A题,十分钟拍完交上去就A了 然后B题写了一发暴力 ...
- 2013ACM暑假集训总结-致将走上大三征途的我
回想起这个暑假,从开始与雄鹰一起的纠结要不要进集训队,与吉吉博博组队参加地大邀请赛,害怕进不了集训队.当时激励我月份开始接触的,记得当时在弄运动会来着,然后就问了雄鹰一些输入输出的东西,怀着满心的期待 ...
- 分西瓜(DFS)
描述今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么庆祝生日,经过调查,zb发现C小加和never都很喜欢吃西瓜,而且一吃就是一堆的那种,zb ...
- zb的生日(暴搜dfs)
zb的生日 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么 ...
随机推荐
- learning java FileInputStream
public class FileInputStreamTest { public static void main(String[] args) throws IOException { var f ...
- CentOS7 通过systemd 添加开机重启服务
现在越来越多的环境采用 CentOS 7 作为基础配置,特别是 Hadoop生态 如果要测试或部署环境需要启动很多组件(zookeeper.kafka.redis等等),如下内容是在操作系统层实现开机 ...
- 20189220 余超《Linux内核原理与分析》第一周作业
实验一 Linux系统简介 通过实验一主要是学习到了Linux 的历史简介,linux与windows之间的区别,主要是免费和收费,软件和支持,安全性,使用习惯,可制定性,应用范畴等.linux具有稳 ...
- 2015-2016-2《Java程序设计》团队博客2
简易画图板介绍 一.功能结构图 二.主类设计 1.总体设计:在设计简易画图板时,根据程序功能的分类,包含了十二个文件,包括SimpleDraw.java,MenuContainer.java,Dra ...
- Servlet相关的几种乱码
1. 页面中文显示乱码 原因: response中的内容会先输入到response缓冲区,然后再输入到传给浏览器,所以要将缓冲区和浏览器的编码都设置成utf-8 1)未使用jsp,而是在servlet ...
- 【软工实践】Beta冲刺(5/5)
链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 将数据分析以可视化形式展示出来 新增数据分析展示等功能API 服务器后端部署, ...
- Sparrow-WiFi:一款Linux平台下的图形化WiFi及蓝牙分析工具
工具概述 Sparrow-wifi本质上一款针对下一代2.4GHz和5GHz的WiFi频谱感知工具,它不仅提供了GUI图形化用户界面,而且功能更加全面,可以代替类似inSSIDer和linssid之类 ...
- em,rem,px的区别,以及实现原理?
px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的.em是相对长度单位.相对于当前对象内文本的字体尺寸举个例子:比如说当前容器`font-size:16px;`则`1em`就 ...
- 运行OpenGL红宝书第9版源码时Visual Studio提示“无法启动程序...ALL_BUILD。拒绝访问“的问题的解决办法
问题描述: OpenGL红宝书第9版源码采用CMake编译后,用相应的Visual Studio(如VS2012)打开“vermilion9.sln”解决方案,并运行时Visual Studio提示“ ...
- Web.config 文件例子
php - 如何使用web.config重写IIS UrlRewriteModule中的网页的URL? <?xml version="1.0" encoding=" ...