Codeforces Beta Round #87 (Div. 1 Only)A. Party(深搜)

传送门

Description

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?

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.

Sample Input

5-1121-1

Sample Output

3

思路

题解:

根据题意只要求出最大的树高即可。

#include<bits/stdc++.h>
using namespace std;
const int maxn =  2005;
vector<int>itv[maxn];
int res = 0;

void dfs(int u,int depth)
{
	res = max(res,depth);
	for (unsigned i = 0;i < itv[u].size();i++)
	{
		dfs(itv[u][i],depth + 1);
	}
}

int main()
{
	int n,p;
	scanf("%d",&n);
	for (int i = 1;i <= n;i++)
	{
		scanf("%d",&p);
		if (p == -1)	itv[0].push_back(i);
		else	itv[p].push_back(i);
	}
	dfs(0,0);
	printf("%d\n",res);
	return 0;
}

 

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2005;
vector<int>itv[maxn];
int level[maxn];

int main()
{
	int n,p;
	scanf("%d",&n);
	for (int i = 1;i <= n;i++)
	{
		scanf("%d",&p);
		if (~p)	itv[p].push_back(i);
		else	itv[0].push_back(i);
	}
	queue<int>que;
	for (unsigned i = 0;i < itv[0].size();i++)
	{
		int u = itv[0][i];
		que.push(u);
		level[u] = 1;
		while (!que.empty())
		{
			u = que.front();
			que.pop();
			for (unsigned j = 0;j < itv[u].size();j++)
			{
				int v = itv[u][j];
				level[v] = level[u] + 1;
				que.push(v);
			}
		}
	}
	int res = 1;
	for (int i = 1;i <= n;i++)	res = max(res,level[i]);
	printf("%d\n",res);
	return 0;
}

  

 

时间: 2024-08-09 14:36:40

Codeforces Beta Round #87 (Div. 1 Only)A. Party(深搜)的相关文章

Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array

E. Lucky Array Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467are not. Petya has an arra

Codeforces Beta Round #12 (Div 2 Only)

Codeforces Beta Round #12 (Div 2 Only) http://codeforces.com/contest/12 A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7 t

Codeforces Beta Round #49 (Div. 2)

Codeforces Beta Round #49 (Div. 2) http://codeforces.com/contest/53 A 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #define

Codeforces Beta Round #72 (Div. 2 Only)

Codeforces Beta Round #72 (Div. 2 Only) http://codeforces.com/contest/84 A 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #de

Codeforces Beta Round #85 (Div. 1 Only) C (状态压缩或是数学?)

C. Petya and Spiders Little Petya loves training spiders. Petya has a board n × m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform it

暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

题目传送门 1 /* 2 题意:求最大矩形(全0)的面积 3 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 4 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 5 详细解释:http://www.cnblogs.com/cszlg/p/3217478.html 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath>

Codeforces Beta Round #6 (Div. 2 Only) B. President&#39;s Office

题目大意 给出一个n*m的矩阵 ,描述桌子的布局.总统的桌子和他的副手的桌子相邻,每一个人的桌子有它独有的颜色.问总统有多少个副手. 解题思路 搜出总统的桌子在矩阵中的边界后判断边界外的其它颜色桌子的数量. 题目代码 #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include

图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces

题目传送门 1 /* 2 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3

BFS Codeforces Beta Round #94 (Div. 2 Only) C. Statues

题目传送门 1 /* 2 BFS:三维BFS,坐标再加上步数,能走一个点当这个地方在步数内不能落到.因为雕像最多8步就会全部下落, 3 只要撑过这个时间就能win,否则lose 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <queue> 8 #include <vector> 9 #include <cstring> 10 using namespace std; 11 1