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在武汉集训.他想给这两位兄弟买点什么 ...
随机推荐
- 洛谷 P3385 【模板】负环 题解
P3385 [模板]负环 题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 寻找一个从顶点1所能到达的负环,负环定义为:一个边权之和为负的环. 输入格式 第一行一个正整数T ...
- CSS — BEM 命名规范
推荐阅读: https://juejin.im/post/5b925e616fb9a05cdd2ce70d 1 什么是 BEM 命名规范 Bem 是块(block).元素(element).修饰符(m ...
- 第09组 团队Git现场编程实战
组长博客链接 1.团队分工 团队成员 分工明细 王耀鑫 博客撰写,数据处理 陈志荣 前端界面,前端功能实现 陈超颖 前端界面,前端功能实现 沈梓耀 前端界面,前端功能实现 林明镇 数据处理 滕佳 前端 ...
- Lomok @Data使用
看了廖师兄的Springboot视频发现很多很好玩的小工具,lombok就是其中一个.lombok是一个可以通过简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 Java 代码的工具,简单来 ...
- 刷题记录:[CISCN 2019 初赛]Love Math
目录 刷题记录:[CISCN 2019 初赛]Love Math 思路一 思路二 总结 刷题记录:[CISCN 2019 初赛]Love Math 题目复现链接:https://buuoj.cn/ch ...
- better-scroll在vue项目中的使用
1.准备工作 在项目中安装better-scroll: npm install --save better-scroll 组件中引入插件 import BScroll from "bette ...
- 2019_软工实践_Beta(2/5)
队名:955 组长博客:点这里! 作业博客:点这里! 组员情况 组员1(组长):庄锡荣 过去两天完成了哪些任务 文字/口头描述 ?按照时间进度的安排进行相应的检查 展示GitHub当日代码/文档签入记 ...
- python 安装setuptools、pip《转》
https://www.jianshu.com/p/e9ab614cad9b 安装setuptools 下载setuptools源码setuptools-25.2.0.tar.gz 地址:https: ...
- Python Selenium Webdriver常用方法总结
Python Selenium Webdriver常用方法总结 常用方法函数 加载浏览器驱动: webdriver.Firefox() 打开页面:get() 关闭浏览器:quit() 最大化窗口: m ...
- Nginx简单配置几个基于端口的虚拟主机
nginx.conf中,一个server段对应一个虚拟主机,如果要增加多个虚拟主机,增加多个server段即可. server { listen ; access_log logs/.log; loc ...