题目链接: http://codeforces.com/problemset/problem/115/A
题目大意: 每个人都有一个或者没有直属上司, 现在想举办一个 party, 这个 party 要求参加的人人人平等不存在上下级关系. 问最少要分几组?
思路:
其实就是每个人都有一个或者没有父亲节点, 我们要让同深度的人组成一个队伍就可以. 即就是找树的最大深度
AC 代码:
- #include <cstdio>
- #include <string>
- #include <iostream>
- #include <algorithm>
- #include <string.h>
- #include <math.h>
- using namespace std;
- const int maxn = 2005;
- int fa[maxn];
- int temp = 0;
- void dfs(int i)
- {
- if (i == -1)
- return ;
- else
- {
- temp++;
- dfs(fa[i]);
- }
- }
- int main()
- {
- int n,sum = 0;
- cin>> n;
- for (int i=1;i<=n;i++)
- {
- cin>> fa[i];
- }
- for (int i=1;i<=n;i++)
- {
- temp = 0;
- dfs(i);
- sum = max(sum,temp);
- }
- printf("%d\n",sum);
- return 0;
- }
来源: http://www.bubuko.com/infodetail-3120924.html