Given a tree, find the node with the minimum sum of distances to other nodes

265 篇文章 1 订阅
9 篇文章 0 订阅

O(n) complexity, have a traversal for the tree. Get the information of all children, then traverse the tree again. 


#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;

class Node {
 public:
  int cnum;
  vector<int> child;
  Node(int num):cnum(num) {
  }
};


int buildTree(const vector<pair<int,int>>& edges,
                   vector<int>& visited, vector<Node>& tree, int cur, int depth, int& sum) {                   
  int size = edges.size(), i;

  for (i = 0; i < size; ++i) 
    if (visited[i] == 0 && (edges[i].first == cur || edges[i].second == cur)) {
      visited[i] = 1;
      int next = edges[i].first == cur ? edges[i].second : edges[i].first;
      sum += depth+1;
      tree[cur].child.push_back(next);
      tree[cur].cnum += buildTree(edges, visited, tree, next,depth+1, sum) + 1;
    }
  return tree[cur].cnum;
}
void dfs(vector<Node>& tree, int root, int& minsum, int& minroot, int cursum) {

  int i, size = tree[root].child.size(), next, n = tree.size(), m;
  for (i = 0; i < size; ++i) {
    next = tree[root].child[i], m = tree[next].cnum;
    int sum = cursum - m + (n - m -2);
    if (sum < minsum) {
      minsum = sum;
      minroot = next;
    }
    dfs(tree, next, minsum, minroot, sum);
  }  
}
int getNode(const vector<pair<int, int>>& edges) {
  int size = edges.size(), root, sum = 0, minroot, minsum, cnum;
  if (size == 0)
    return 0;
  vector<Node> tree(size+1, Node(0));    
  vector<int> edgevisited(size, 0);
  
  root= edges[0].first, minroot = root;
  cnum = buildTree(edges, edgevisited, tree, root, 0, sum);
  minsum = sum;
  dfs(tree, root, minsum, minroot, sum);
  return minroot;
}
int main() {
  vector<pair<int, int>> edges;
  edges.push_back(make_pair(0,1));
  edges.push_back(make_pair(0,2));
  edges.push_back(make_pair(4,3));
  edges.push_back(make_pair(1,3));
  edges.push_back(make_pair(1,5));
  edges.push_back(make_pair(6,5));
  int res = getNode(edges);
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值