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在武汉集训.他想给这两位兄弟买点什么 ...
随机推荐
- nginx 添加用户认证
nginx 添加用户认证 nginx 配置文件添加: 配置代理添加用户认证:server { listen ; server_name localhost; location ...
- JMeter学习1
Jmeter的组织方式相对比较扁平,直接是TestPlan(相当于Project),TestPlan下创建的ThreadsGroup(相当于TestCase), Jmeter一个TestPlan也是一 ...
- 【JZOJ6227】【20190621】ichi
题目 $n , m ,d,x\le 10^5 , $强制在线 题解 对原树做dfs,得到原树的dfs序 对kruksal重构树做dfs,得到重构树的dfs序 那么就是一个三维数点问题 强制在线并且卡空 ...
- javascript使用history api防止|阻止页面后退
奇葩需求啥时候都会有,最近有个需求是不允许浏览器回退,但是所有页面都是超链接跳转,于是乎脑壳没转弯就回答了做不到,结果尼玛被打脸了,这打脸的声音太响,终于静下心来看了下history api. 先上代 ...
- 刷题记录:[ByteCTF 2019]EZCMS
目录 刷题记录:[ByteCTF 2019]EZCMS 一.知识点 1.源码泄露 2.MD5长度扩展攻击 3.php://filter绕过正则实现phar反序列化 刷题记录:[ByteCTF 2019 ...
- Linux安装问题解决
首先安装VM,之后下载centos7.5 安装好vm后运行时无法执行操作 提示 已将该虚拟机配置为使用 64 位客户机操作系统.但是,无法执行 64 位操作. 此主机支持 Intel VT-x,但 I ...
- 搭建阿里云服务 FTP 折中方案
该配置的服务都配置了,端口也都打开了 ,但是ftp 就是连接不上 就是打不开目录 8uftp 出现以下情况 配置文件逐条检查,端口逐个检查 都没有问题,还是出现这种情况,实在没辙,蛋疼...... ...
- jmeter元件作用及执行顺序
jmeter是一个开源的性能测试工具,它可以通过鼠标拖拽来随意改变元件之间的顺序以及元件的父子关系,那么随着它们的顺序和所在的域不同,它们在执行的时候,也会有很多不同. jmeter的test pla ...
- Spring Boot源码中模块详解
Spring Boot源码中模块详解 一.源码 spring boot2.1版本源码地址:https://github.com/spring-projects/spring-boot/tree/2.1 ...
- Java架构师之路(参考这个学习吧)