• 1021 Deepest Root


    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains a positive integer N (≤104) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N−1 lines follow, each describes an edge by given the two adjacent nodes' numbers.

    Output Specification:

    For each test case, print each of the deepest roots in a line. If such a root is not unique, print them in increasing order of their numbers. In case that the given graph is not a tree, print Error: K components where K is the number of connected components in the graph.


    Sample Input 1:

    1. 5
    2. 1 2
    3. 1 3
    4. 1 4
    5. 2 5

    Sample Output 1:

    1. 3
    2. 4
    3. 5

    Sample Input 2:

    1. 5
    2. 1 3
    3. 1 4
    4. 2 5
    5. 3 4

    Sample Output 2:

    Error: 2 components

    题目大意

    给定一个无向图,判断其是否能构成一棵树。如果不能,输出其连通分量的个数;如果能,求构成最大深度树时的根节点,如果该节点不唯一,降序输出。


    思路

    查并集求得连通分量个数,不唯1不是树。

    将所有节点间连通数量为一的节点,选入DFS暴力判断

    依次求得最大深度即可

    ps: 注意N=1的情况


    C/C++ 

    1. #include
    2. using namespace std;
    3. vector<int> road[10001];
    4. int N,a,b;
    5. bool appear[10001];
    6. void findUnion(int now); // 查并集
    7. void findDeep(int now,int deep,int *maxDeep); // 寻找最大的深度
    8. int main()
    9. {
    10. cin >> N;
    11. for(int z=1;z
    12. cin >> a >> b;
    13. road[a].push_back(b);
    14. road[b].push_back(a);
    15. }
    16. vector<int> keys,result;
    17. int flag = 0;
    18. for(int z=1;z<=N;z++){
    19. if(road[z].size()==1) keys.push_back(z);
    20. if(appear[z]) continue;
    21. findUnion(z);
    22. flag++;
    23. }
    24. if(N==1) cout << 1 << endl;
    25. else if(flag>1) printf("Error: %d components\n",flag);
    26. else
    27. {
    28. memset(appear,0,sizeof appear);
    29. int deepKey=0,maxDeep;
    30. for(int x:keys){
    31. maxDeep = 0;
    32. findDeep(x,1,&maxDeep);
    33. if(maxDeep>deepKey) result.clear(),deepKey = maxDeep;
    34. if(maxDeep==deepKey) result.push_back(x);
    35. }
    36. for(int x:result) cout << x << endl;
    37. }
    38. return 0;
    39. }
    40. void findUnion(int now){
    41. if(appear[now]) return;
    42. appear[now] = true;
    43. for(int x:road[now]) findUnion(x);
    44. }
    45. void findDeep(int now,int deep,int *maxDeep){
    46. if(appear[now]) return;
    47. appear[now] = true;
    48. *maxDeep = max(*maxDeep,deep);
    49. for(int x:road[now]) findDeep(x,deep+1,maxDeep);
    50. appear[now] = false;
    51. }

  • 相关阅读:
    今天面试招了个18K的人,从腾讯出来的果然都有两把刷子···
    自定义mvc增删改查
    数据科学最佳实践:Kedro 的工程化解决方案 | 开源日报 No.47
    dubbo+zookeeper环境配置及搭建
    机器学习从入门到放弃:我们究竟是怎么教会机器自主学习的?
    [二进制学习笔记]LibcSearcher安装
    windows本地搭建mmlspark分布式机器平台流程
    计网--应用层
    CTF-php特性绕过
    springboot+jsp项目校园外卖配送系统
  • 原文地址:https://blog.csdn.net/daybreak_alonely/article/details/127689477