Advertisement
wurdalak007

new

Dec 11th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. /*Дано невзвешенное дерево. Расстоянием между двумя вершинами будем называть количество ребер в пути, соединяющем эти две вершины. Для каждой вершины определите сумму расстояний до всех остальных вершин. Время работы должно быть O(n).*/
  2. #include <iostream>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. class Tree {
  10. public:
  11. Tree( int n );
  12. void insert( int first, int second );
  13. void restore_dist( int parent_pos );
  14. void solve();
  15.  
  16. private:
  17.  
  18. struct Node{
  19. vector <Node*> children;
  20. int i;
  21. int depth = 0;
  22. int count_of_child = 0;
  23. };
  24.  
  25. int distance( int pos );
  26. int restore_vertex_sum ( int pos );
  27. int count_of_child( int pos );
  28.  
  29. vector<Node> root;
  30. };
  31.  
  32. Tree::Tree( int n ) :
  33. root(n)
  34. {
  35. for( int i = 0; i < n; i++ ) {
  36. root[i].i = i;
  37. }
  38. }
  39.  
  40. void Tree::solve() {
  41. count_of_child( 0 );
  42. restore_vertex_sum( 0 );
  43. restore_dist( 0 );
  44.  
  45. for( int i = 0; i < root.size(); i++ ) {
  46. cout << root[i].depth << endl;
  47. }
  48. }
  49.  
  50. int Tree::count_of_child( int pos ) {
  51. for( int i = 0; i < root[pos].children.size(); i++ ) {
  52. root[pos].count_of_child += 1 + count_of_child( root[pos].children[i]->i );
  53. }
  54. return root[pos].count_of_child;
  55. }
  56.  
  57. void Tree::restore_dist( int parent_pos ) {
  58. queue< pair<int,int> > q;
  59.  
  60. for( int i = 0; i < root[parent_pos].children.size(); i++ ) {
  61. q.push(make_pair(parent_pos, root[parent_pos].children[i]->i));
  62. }
  63.  
  64. while( !q.empty() ) {
  65. pair<int, int> elem = q.front();
  66. q.pop();
  67. for( int i = 0; i < root[elem.second].children.size(); i++ ) {
  68. q.push(make_pair(elem.second, root[elem.second].children[i]->i));
  69. }
  70. root[elem.second].depth = root[elem.first].depth - 2*root[elem.second].count_of_child + root[elem.first].count_of_child - 1;
  71. root[elem.second].count_of_child += root[elem.first].count_of_child - root[elem.second].count_of_child;
  72. }
  73.  
  74. }
  75.  
  76. int Tree::restore_vertex_sum( int pos ) {
  77. root[pos].depth = 0;
  78. for( int i = 0; i < root[pos].children.size(); i++ ) {
  79. root[pos].depth += 1 + restore_vertex_sum(root[pos].children[i]->i) + root[pos].children[i]->count_of_child;
  80. }
  81. return root[pos].depth;
  82. }
  83.  
  84. void Tree::insert( int first, int second ) {
  85. root[min(first,second)].children.push_back(&root[max(first,second)]);
  86. }
  87.  
  88. int main() {
  89. int n = 0;
  90. cin >> n;
  91. Tree tree(n);
  92.  
  93. for( int i = 0; i < n - 1; i++ ) {
  94. int x = 0;
  95. int y = 0;
  96. cin >> x >> y;
  97. tree.insert(x, y);
  98. }
  99.  
  100. tree.solve();
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement