• 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. }

  • 相关阅读:
    小波神经网络短期负荷分析,小波神经网络的缺点
    IPV4优先于IPV6设置
    总结:nginx配置
    nginx配置中$http_host、$host、$host:$proxy_port和$host:$server_port区别
    ICME 会议介绍
    Java继承的本质分析
    fdbus之事件循环及线程关系
    前端面试题:html和css面试题
    许战海战略文库|无增长则衰亡:中小型制造企业增长困境
    简单好用的苹果手机软件数据备份软件
  • 原文地址:https://blog.csdn.net/daybreak_alonely/article/details/127689477