CF

A. Party

time limit per test3 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

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.

Examples

inputCopy

5

-1

1

2

1

-1

outputCopy

3

Note

For the first example, three groups are sufficient, for example:

Employee 1

Employees 2 and 4

Employees 3 and 5

【分析】:若是并查集,不要路径压缩,因为要记录深度。输出最大深度即可。

[DFS]

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1e5 + 10;
  4. const int mod = 142857;
  5. const int inf = 0x3f3f3f3f;
  6. int n,vis[maxn],cnt,x,ans;
  7. vector<int> G[maxn];
  8. void dfs(int root, int cur)
  9. {
  10. ans=max(ans,cur);
  11. for(int i=0;i<G[root].size();i++)
  12. {
  13. vis[G[root][i]]=1;
  14. dfs(G[root][i],cur+1);
  15. }
  16. }
  17. int main()
  18. {
  19. while(~scanf("%d",&n))
  20. {
  21. memset(vis,0,sizeof(vis));
  22. for(int i=0;i<=n;i++) G[i].clear();
  23. for(int i=1;i<=n;i++)
  24. {
  25. scanf("%d",&x);
  26. if(x!=-1) G[x].push_back(i);
  27. }
  28. ans=0;
  29. for(int i=1;i<=n;i++)
  30. {
  31. if(!vis[i])
  32. {
  33. vis[i]=1;
  34. dfs(i,1);
  35. }
  36. }
  37. printf("%d\n",ans);
  38. }
  39. }
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1e5 + 10;
  4. const int mod = 142857;
  5. const int inf = 0x3f3f3f3f;
  6. int n,fa[maxn],cnt,x,ans;
  7. vector<int> G[maxn];
  8. void dfs(int i)
  9. {
  10. if(i==-1) return;
  11. else
  12. {
  13. x++;
  14. dfs(fa[i]);
  15. }
  16. }
  17. int main()
  18. {
  19. while(~scanf("%d",&n))
  20. {
  21. for(int i=1;i<=n;i++)
  22. {
  23. scanf("%d",&fa[i]);
  24. }
  25. ans=0;
  26. for(int i=1;i<=n;i++)
  27. {
  28. x=0;
  29. dfs(i);
  30. ans=max(ans,x);
  31. }
  32. printf("%d\n",ans);
  33. }
  34. }

[并查集]

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1e5 + 10;
  4. const int mod = 142857;
  5. const int inf = 0x3f3f3f3f;
  6. int n,fa[maxn],cnt,x,ans;
  7. vector<int> G[maxn];
  8. void init(int n)
  9. {
  10. for(int i=1;i<=n;i++)
  11. fa[i]=i;
  12. }
  13. void Find(int x)
  14. {
  15. if(x==fa[x])
  16. return ;
  17. cnt++;
  18. Find(fa[x]);
  19. }
  20. void join(int x,int y)
  21. {
  22. fa[x]=y;
  23. return;
  24. }
  25. int main()
  26. {
  27. while(~scanf("%d",&n))
  28. {
  29. for(int i=1;i<=n;i++)
  30. {
  31. scanf("%d",&x);
  32. if(x!=-1)
  33. join(i,x);
  34. }
  35. ans=0;
  36. for(int i=1;i<=n;i++)
  37. {
  38. cnt=0;
  39. Find(i);
  40. ans=max(ans,cnt);
  41. }
  42. printf("%d\n",ans);
  43. }
  44. }

CF 115 A 【求树最大深度/DFS/并查集】的更多相关文章

  1. 求树的直径+并查集(bfs,dfs都可以)hdu4514

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 这题主要是叫我们求出树的直径,在求树的直径之前要先判断一下有没有环 树的直径指的就是一棵树上面距 ...

  2. 【bzoj2870】最长道路tree 树的直径+并查集

    题目描述 给定一棵N个点的树,求树上一条链使得链的长度乘链上所有点中的最小权值所得的积最大. 其中链长度定义为链上点的个数. 输入 第一行N 第二行N个数分别表示1~N的点权v[i] 接下来N-1行每 ...

  3. 【CF938G】Shortest Path Queries(线段树分治,并查集,线性基)

    [CF938G]Shortest Path Queries(线段树分治,并查集,线性基) 题面 CF 洛谷 题解 吼题啊. 对于每个边,我们用一个\(map\)维护它出现的时间, 发现询问单点,边的出 ...

  4. [BZOJ3038]上帝造题的七分钟2 树状数组+并查集

    考试的时候用了两个树状数组去优化,暴力修改,树状数组维护修改后区间差值还有最终求和,最后骗了40分.. 这道题有好多种做法,求和好说,最主要的是开方.这道题过的关键就是掌握一点:在数据范围内,最多开方 ...

  5. hdu 5458 Stability(树链剖分+并查集)

    Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total ...

  6. 【BZOJ4025】二分图(线段树分治,并查集)

    [BZOJ4025]二分图(线段树分治,并查集) 题面 BZOJ 题解 是一个二分图,等价于不存在奇环. 那么直接线段树分治,用并查集维护到达根节点的距离,只计算就好了. #include<io ...

  7. 【loj6038】「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT

    题目描述 给你 $n$ 个点,支持 $m$ 次操作,每次为以下两种:连一条边,保证连完后是一棵树/森林:询问一个点能到达的最远的点与该点的距离.强制在线. $n\le 3\times 10^5$ ,$ ...

  8. cf1278D——树的性质+并查集+线段树/DFS判环

    昨天晚上本来想认真打一场的,,结果陪女朋友去了.. 回来之后看了看D,感觉有点思路,结果一直到现在才做出来 首先对所有线段按左端点排序,然后用并查集判所有边是否联通,即遍历每条边i,和前一条不覆盖它的 ...

  9. 51nod1307(暴力树剖/二分&dfs/并查集)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数 ...

随机推荐

  1. Java IO 小结

    Java IO 的学习需要明白流设计的体系结构,这样才能在实际需要的时候,通过API文档查阅,快速实现功能.

  2. Netscaler重置密码的方法

    Netscaler重置密码的方法 http://blog.51cto.com/caojin/1898401 有时候我们会碰到忘记Netscaler的密码,或接手别人的设备而不知道密码的情况.在这种情况 ...

  3. 【转】Word单引号‘’替换为正确的单引号(plsql参数的单引号)

    转自 http://jingyan.baidu.com/article/39810a23db44b5b636fda6f2.html 问题描述:   单引号明显不一样,替换不了 解决方案,如下图

  4. BZOJ 4777 Usaco2017 Open Switch Grass Kruskal+替罪羊树+权值线段树

    这道题首先可以看出答案一定是一条边,而且答案一定在最小生成树上,那么我们就可以在这个最小生成树上维护他与异色儿子的边最小值,所以我们就可以已通过Kruskal和一棵平衡树来解决,时间复杂度是O(n*l ...

  5. 微信小程序base64编码解码以及utf-8解码

    function base64_encode (str) { // 编码,配合encodeURIComponent使用 var c1, c2, c3; var base64EncodeChars = ...

  6. HTTP请求中同步与异步有什么不同

    普通的B/S模式就是同步,而AJAX技术就是异步,当然XMLHttpReques有同步的选项. 同步:提交请求->等待服务器处理->处理完毕返回.这个期间客户端浏览器不能干任何事. 异步: ...

  7. WCF分布式开发步步为赢(13):WCF服务离线操作与消息队列MSMQ

    之前曾经写过一个关于MSMQ消息队列的文章:WCF分布式开发必备知识(1):MSMQ消息队列 ,当时的目的也是用它来作为学习WCF 消息队列MSMQ编程的基础文章.在那篇文章里,我们详细介绍了MSMQ ...

  8. C/C++常考面试题(二)

    网上看到的面经,说是dynamic_cast的实现,和RTTI的相关,这才发现原来对这个概念这么模糊,所以作了这个总结. C/C++常考面试题(二) RTTI(Runtime Type Informa ...

  9. lhgdialog的传值问题

    一前言 今天就离职了,顺便把还没有记载下来得Js有关知识给记载下来,其实这个是lhgdialog.js中的传值问题.就是弹出框选择数据后加载到父页面上,自己用html做了测试. 二:内容 html代码 ...

  10. Visual Studio Code 配置C/C++环境

    0. 前言 VS Code 是微软发布一款跨平台的源代码编辑器,其拥有强大的功能和丰富的扩展,使之能适合编写许多语言. 本文面向初学者(但不是纯小白),分享一点我配置C/C++的经验. 本文所有内容均 ...