(PAT 1021) Deepest Root (广度优先遍历求层数)

柔情只为你懂 2022-04-23 07:10 246阅读 0赞

A graph which is connected and acyclic can be considered a tree. The hight 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:

  1. Error: 2 components

解题思路:

首先连通分量可以通过广度优先遍历去求,对每一个结点进行广度优先遍历,每一次遍历完成后,使图的连通分量加1,直到所有结点都被访问过为止

然后是求Deepest root,对每一个结点进行广度优先遍历,得到以该节点出发遍历图所得的到的层数,最后取层数最大的那几个结点即可。

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <string.h>
  5. #include <queue>
  6. using namespace std;
  7. //广度优先求高度,广度优先求连通分量
  8. struct nNode {
  9. int ndata;
  10. int hights;
  11. };
  12. class Graphic {
  13. public:
  14. int n;
  15. vector<int>* edges;
  16. bool* visited;
  17. vector<int> res;
  18. public:
  19. Graphic(int _n) {
  20. n = _n;
  21. edges = new vector<int>[n];
  22. visited = new bool[n];
  23. memset(visited, 0, n);
  24. }
  25. void ginsert(int x, int y) {
  26. edges[x].push_back(y); //有向图插入
  27. edges[y].push_back(x);
  28. }
  29. int getComponents() {
  30. int k = 0;
  31. queue<int> Bfs_queue;
  32. for (int i = 1; i <= n-1; ++i) {
  33. if (visited[i]) continue;
  34. visited[i] = true;
  35. Bfs_queue.push(i);
  36. while (!Bfs_queue.empty()) {
  37. int tnode = Bfs_queue.front();
  38. Bfs_queue.pop();
  39. for (int adj_edge : edges[tnode]) {
  40. if (!visited[adj_edge]) {
  41. visited[adj_edge] = true;
  42. Bfs_queue.push(adj_edge);
  43. }
  44. }
  45. }
  46. k++;
  47. }
  48. memset(visited, 0, n);
  49. return k;
  50. }
  51. void getHight() {
  52. int maxHight = 0;
  53. queue<int> bfs_queue;
  54. queue<int> layer_queue;
  55. for (int i = 1; i <= n-1; ++i) {
  56. int curLayer = 0;
  57. visited[i] = true;
  58. bfs_queue.push(i);
  59. layer_queue.push(curLayer);
  60. while (!bfs_queue.empty()) {
  61. int tnode = bfs_queue.front();
  62. curLayer = layer_queue.front() + 1;
  63. bfs_queue.pop();
  64. layer_queue.pop();
  65. for (int adj_edge : edges[tnode]) {
  66. if (!visited[adj_edge]) {
  67. visited[adj_edge] = true;
  68. bfs_queue.push(adj_edge);
  69. layer_queue.push(curLayer);
  70. }
  71. }
  72. }
  73. if (curLayer >= maxHight) {
  74. if (curLayer > maxHight) {
  75. maxHight = curLayer;
  76. res.clear();
  77. res.push_back(i);
  78. }
  79. else if (curLayer == maxHight) {
  80. res.push_back(i);
  81. }
  82. }
  83. memset(visited, 0, n);
  84. }
  85. }
  86. };
  87. int main() {
  88. int N;
  89. cin >> N;
  90. Graphic graJic(N + 1);
  91. for (int i = 0; i < N - 1; ++i) {
  92. int nx, ny;
  93. cin >> nx >> ny;
  94. graJic.ginsert(nx, ny);
  95. }
  96. int cop = graJic.getComponents();
  97. if (cop > 1) { //直接报错
  98. printf("Error: %d components\n", cop);
  99. }
  100. else {
  101. graJic.getHight();
  102. sort(graJic.res.begin(), graJic.res.end());
  103. for (auto x : graJic.res) {
  104. cout << x << endl;
  105. }
  106. }
  107. return 0;
  108. }

发表评论

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

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

相关阅读

    相关 java广度优先

    / 图的广度优先遍历 从1号顶点开始按照广度优先遍历顺序输出图G中的每个顶点,顶点编号间用一个空格间隔,每个无向图的输出占用一行,最