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

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;
}

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

时间: 2024-08-05 06:06:40

Given a tree, find the node with the minimum sum of distances to other nodes的相关文章

834. Sum of Distances in Tree —— weekly contest 84

Sum of Distances in Tree An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ import java.util.*; public class Solution { public int run(TreeNode root) { if(root == null){ return 0; } Q

[Swift]LeetCode834. 树中距离之和 | Sum of Distances in Tree

An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0]and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Exam

[LeetCode] 834. Sum of Distances in Tree 树中距离之和

An undirected, connected?tree with?N?nodes labelled?0...N-1?and?N-1?edges?are?given. The?ith edge connects nodes?edges[i][0]?and?edges[i][1]?together. Return a list?ans, where?ans[i]?is the sum of the distances between node?i?and all other nodes. Exa

leetcode834 Sum of Distances in Tree

思路: 树形dp. 实现: 1 class Solution 2 { 3 public: 4 void dfs(int root, int p, vector<vector<int>>& G, vector<int>& cnt, vector<int>& res) 5 { 6 for (auto it: G[root]) 7 { 8 if (it == p) continue; 9 dfs(it, root, G, cnt, res)

Strategic Game(匈牙利算法)

Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6421    Accepted Submission(s): 2987 Problem Description Bob enjoys playing computer games, especially strategic games, but somet

POJ 1463 树状dp

Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 6629   Accepted: 3058 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad

hdu1054最小顶点覆盖

最小定点覆盖是指这样一种情况: 图G的顶点覆盖是一个顶点集合V,使得G中的每一条边都接触V中的至少一个顶点.我们称集合V覆盖了G的边.最小顶点覆盖是用最少的顶点来覆盖所有的边.顶点覆盖数是最小顶点覆盖的大小. 相应地,图G的边覆盖是一个边集合E,使得G中的每一个顶点都接触E中的至少一条边.如果只说覆盖,则通常是指顶点覆盖,而不是边覆盖. 在二分图中:最大匹配数=最小顶点覆盖数: Bob enjoys playing computer games, especially strategic gam

poj 1463(树形DP)

Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 7584   Accepted: 3518 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad