首页 > 其他 > 详细

1021. Deepest Root (25)

时间:2015-12-06 11:28:55      阅读:125      评论:0      收藏:0      [点我收藏+]
首先计算连通分量。dfs
然后分两种情况,n数目很小的时候对每个节点dfs,n大的时候超时
n数目大的时候计算只具有单边的节点数。n小的时候答案错误
两者结合就好了,
当然这是一种不可取的办法。。。。。

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 (<=10000) 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:
5
1 2
1 3
1 4
2 5
Sample Output 1:
3
4
5
Sample Input 2:
5
1 3
1 4
2 5
3 4
Sample Output 2:
Error: 2 components

 
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. using namespace std;
  5. int n;
  6. int a[10001][10001];
  7. int mark[10001];
  8. int components = 0;
  9. int maxLevel = 0;
  10. vector<int> elements;
  11. void dfs(int root, int i, int level) {
  12. if (level > maxLevel) {
  13. maxLevel = level;
  14. elements.clear();
  15. elements.push_back(root);
  16. }
  17. else if (level == maxLevel) {
  18. if (root != elements[elements.size() - 1])
  19. elements.push_back(root);
  20. }
  21. for (int j = 1; j <= n; j++) {
  22. if (a[j][i] > 0 && j != i&&mark[j] == 0) {
  23. mark[j] = 1;
  24. dfs(root, j, level + 1);
  25. }
  26. }
  27. }
  28. void CalcComp(int p) {
  29. for (int i = 1; i <= n; i++) {
  30. if (a[p][i] > 0 && p != i&&mark[i] == 0) {
  31. mark[i] = 1;
  32. CalcComp(i);
  33. }
  34. }
  35. }
  36. int main(void) {
  37. cin >> n;
  38. for (int i = 1; i < n; i++) {
  39. int u, v;
  40. cin >> u >> v;
  41. a[u][v] = 1;
  42. a[v][u] = 1;
  43. }
  44. while (true)
  45. {
  46. int p, cnt = 0;
  47. for (int i = 1; i <= n; i++) {
  48. if (mark[i] == 0) {
  49. components++;
  50. mark[i]++;
  51. p = i;
  52. break;
  53. }
  54. else {
  55. cnt++;
  56. }
  57. }
  58. if (cnt == n) {
  59. cnt = 0;
  60. break;
  61. }
  62. CalcComp(p);
  63. }
  64. if (components > 1) {
  65. cout << "Error: " << components << " components";
  66. return 0;
  67. }
  68. if (n <= 15) {
  69. for (int i = 1; i <= n; i++) {
  70. for (int j = 1; j <= n; j++)
  71. mark[j] = 0;
  72. mark[i] = 1;
  73. dfs(i, i, 1);
  74. }
  75. }
  76. else {
  77. for (int i = 1; i <= n; i++) {
  78. int cnt = 0;
  79. for (int j = 1; j <= n; j++) {
  80. cnt += a[i][j];
  81. }
  82. if (cnt == 1)
  83. elements.push_back(i);
  84. cnt = 0;
  85. }
  86. }
  87. for (int i = 0; i < elements.size(); i++) {
  88. cout << elements[i] << endl;
  89. }
  90. return 0;
  91. }





1021. Deepest Root (25)

原文:http://www.cnblogs.com/zzandliz/p/5023075.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!