codeforces 115A A. Party
http://codeforces.com/problemset/problem/115/A 题意:给出n个人之间的上级下级关系。问如何分得最少的组,使得没一组中的人不存在上下级关系。 思路:用树的观点来考虑会很容易。可以看成给了一棵森冷。对于不同的树的相同层的点,不存在上下级关系,可以放在一个group.对于同一棵树,每一层要单独放一个group.所以答案是所有树的深度的最大值。
1/* ***********************************************
2Author :111qqz
3Created Time :2015年12月04日 星期五 21时31分38秒
4File Name :code/cf/problem/115A.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25
26
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33
34const int N=2E3+7;
35int p[N];
36int f[N];
37
38
39int n;
40int ans;
41int main()
42{
43 #ifndef ONLINE_JUDGE
44 freopen("code/in.txt","r",stdin);
45 #endif
46
47 scanf("%d",&n);
48 for ( int i = 1 ; i <= n ; i++) scanf("%d",&p[i]);
49 ans = -1;
50 for ( int i = 1 ; i <= n ; i++)
51 {
52 int depth = 1;
53 int id = i;
54 while (p[id]!=-1)
55 {
56 depth++;
57 id = p[id];
58 }
59 if (depth>ans) ans = depth;
60 }
61 printf("%d\n",ans);
62
63
64 #ifndef ONLINE_JUDGE
65 fclose(stdin);
66 #endif
67 return 0;
68}