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在武汉集训.他想给这两位兄弟买点什么 ...
随机推荐
- 回文数 js 解法
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- vue 把后端返回的图片和url链接生成的二维码用canvas 合成一张图片
H5 页面在做某个活动的时候,有两种分享方式,一种是链接分享,一种是图片分享. 链接分享的话,如果是在微信里,就可引导用户利用微信浏览器自带的分享,根据sdk设置分享标题简介链接缩略图即可. 图片分享 ...
- 前端微信小程序生鲜类仿爱鲜蜂微信小程序
需求描述及交互分析设计思路和相关知识点首页界面布局设计闪送超市纵向导航设计商品添加到购物车设计购物车商品显示设计收货地址列表显示设计新增收货地址设计 交互分析(1)底部标签导航有首页.闪送超市.购物车 ...
- (4.1)打造简单OS-小实验[图形显示]
主要是实现<简单打造OS>第四小节说到的一个图形界面的实验项目 1.mbr boot.inc ;------------- loader和kernel ---------- LOADER_ ...
- vue中使用vue-pdf插件显示pdf
最近项目需求需要在vue中展示pdf,上网搜索了实现方法,找到vue-pdf这个插件非常好用,并且还有许多方法.属性能进行功能扩展. 一.安装 npm install --save vue-pdf 二 ...
- OpenFOAM——具有压差且平行平板间具有相对运动流动
本算例翻译整理自:http://the-foam-house5.webnode.es/products/chapter-1-plane-parallel-plates-case/ 这个算例中两平板间具 ...
- HTTP/1.0和HTTP/1.1 http2.0的区别,HTTP怎么处理长连接(阿里)
HTTP1.0 HTTP 1.1主要区别 长连接 HTTP 1.0需要使用keep-alive参数来告知服务器端要建立一个长连接,而HTTP1.1默认支持长连接. HTTP是基于TCP/IP协议的,创 ...
- 【大数据】分布式文件系统HDFS 练习
作业要求来自于https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3292 利用Shell命令与HDFS进行交互 以”./bin/dfs d ...
- Android自动化测试--monkey总结
什么是 Monkey Monkey 是一个 Android 自动化测试小工具.主要用于Android 的压力测试, 主要目的就是为了测试app 是否会Crash. Monkey 特点 顾名思义,Mon ...
- Empirical Analysis of Beam Search Performance Degradation in Neural Sequence Models
Empirical Analysis of Beam Search Performance Degradation in Neural Sequence Models 2019-06-13 10:2 ...