URAL - 1056(DFS)

Dear 丶 2022-06-04 05:15 292阅读 0赞

问题描述:

Background

Computer net is created by consecutive computer plug-up to one that has already been connected to the net. Each new computer gets an ordinal number, but the protocol contains the number of its parent computer in the net. Thus, protocol consists of several numbers; the first of them is always 1, because the second computer can only be connected to the first one, the second number is 1 or 2 and so forth. The total quantity of numbers in the protocol is N − 1 ( N is a total number of computers). For instance, protocol 1, 1, 2, 2 corresponds to the following net:







  1. 1 - 2 - 5
    | |
    3 4

The distance between the computers is the quantity of mutual connections (between each other) in chain. Thus, in example mentioned above the distance between computers #4 and #5 is 2, and between #3 and #5 is 3.

Definition. Let the center of the net be the computer which has a minimal distance to the most remote computer. In the shown example computers #1 and #2 are the centers of the net.

Problem

Your task is to find all the centers using the set protocol.

Input

The first line of input contains an integer N, the quantity of computers (2 ≤ N ≤ 10000). Successive N − 1 lines contain protocol.

Output

Output should contain ordinal numbers of the determined net centers in ascending order.

5

1

1

2

2

1 2

题目题意:题目给我们n个点,和他们的连接方式,问那些顶点到图的最远顶点的距离是最短的。

题目分析:图比较大,用vector或者邻接表存储,枚举每个顶点去DFS找到最远距离,保存最小值的顶点

代码如下:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<vector>
  5. #include<cstring>
  6. using namespace std;
  7. const int maxn=1e4+100;
  8. bool vis[maxn];
  9. int deep[maxn];
  10. vector<int> vec[maxn],ans;
  11. int dfs(int root)
  12. {
  13. int ans=0;
  14. if (vec[root].size()==0) return ans;
  15. for (int i=0;i<vec[root].size();i++) {
  16. if (vis[vec[root][i]]) continue;
  17. vis[vec[root][i]]=true;
  18. ans=max(ans,dfs(vec[root][i])+1);
  19. }
  20. return ans;
  21. }
  22. int main()
  23. {
  24. int n;
  25. while (scanf("%d",&n)!=EOF) {
  26. for (int i=2;i<=n;i++) {
  27. int fa;
  28. scanf("%d",&fa);
  29. vec[fa].push_back(i);
  30. vec[i].push_back(fa);
  31. }
  32. int Min=0x3f3f3f3f;
  33. for (int i=1;i<=n;i++) {
  34. memset (vis,false,sizeof (vis));
  35. vis[i]=true;
  36. deep[i]=dfs(i);
  37. Min=min(Min,deep[i]);
  38. }
  39. for (int i=1;i<=n;i++) {
  40. if (deep[i]==Min)
  41. ans.push_back(i);
  42. }
  43. for (int i=0;i<ans.size();i++) {
  44. if (i==ans.size()-1) printf("%d\n",ans[i]);
  45. else printf("%d ",ans[i]);
  46. }
  47. }
  48. return 0;
  49. }

发表评论

表情:
评论列表 (有 0 条评论,292人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Trie树 POJ 1056

    Trie树提供给了一种能够在字符串的长度n时间内判断出来是否在已有集合中已经存在这个字符串了。 1056是判断前缀码的问题。如果所有字符串都不是其他的字符串的前缀的话,那么就

    相关 URAL--1007 codewords

    三种信息传递过程中可能出错的方式,让你恢复原来正确的信息。 我用的是分类暴搜,用字符串模拟的,这里还是是可行的,不过debug快坑死了o(╯□╰)o...... 后面还有一

    相关 URAL 1029

    题目大意:M层N列的矩阵(各元素均为正整数),找出一个路径从第一层到达第M层,使得路径上的所有数的和是所有可达路径中最小的,每次上到下一层以后就不能再上去,依次输出路径上的各点