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.

Example 1:

  1. Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]]
  2. Output: [8,12,6,10,10,10]
  3. Explanation:
  4. Here is a diagram of the given tree:
  5. 0
  6. / \
  7. 1 2
  8. /|\
  9. 3 4 5
  10. We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5)
  11. equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on.

Note: 1 <= N <= 10000

  1. 1 class Solution {
  2. 2 public:
  3. 3 //相当于图来存来处理
  4. 4 vector<vector<int>> tree;
  5. 5 vector<int> res;
  6. 6 vector<int> subc;
  7. 7 int n;
  8. 8 vector<int> sumOfDistancesInTree(int N, vector<vector<int>>& edges) {
  9. 9 //tree.rvec(N,vector<int>(N));
  10. 10 //init
  11. 11 n = N;
  12. 12 tree.resize(N); //初始化的函数
  13. 13 res.assign(N,0);
  14. 14 subc.assign(N,0);
  15. 15 //build tree
  16. 16 for(auto e : edges){ //遍历的技巧
  17. 17 tree[e[0]].push_back(e[1]);
  18. 18 tree[e[1]].push_back(e[0]);
  19. 19 }
  20. 20 set<int> visited1;
  21. 21 set<int> visited2;
  22. 22 DFS_POST(0,visited1); //初始root任何值都行
  23. 23 DFS_PRE(0,visited2);
  24. 24 return res;
  25. 25
  26. 26 }
  27. 27 void DFS_POST(int root,set<int> &visited){ //传引用保存修改值
  28. 28 visited.insert(root);
  29. 29 for(auto i : tree[root]){
  30. 30 if(visited.find(i) == visited.end() ){
  31. 31 DFS_POST(i,visited);
  32. 32 subc[root] += subc[i];
  33. 33 res[root] += res[i] + subc[i];
  34. 34 }
  35. 35 }
  36. 36 subc[root]++; //加上自身节点
  37. 37 }
  38. 38 void DFS_PRE(int root,set<int> &visited){
  39. 39 visited.insert(root);
  40. 40 for(auto i : tree[root]){
  41. 41 if(visited.find(i) == visited.end()){
  42. 42 res[i] = res[root] - subc[i] + n - subc[i]; //算法核心
  43. 43 DFS_PRE(i,visited);
  44. 44 }
  45. 45 }
  46. 46 }
  47. 47
  48. 48 };

主要函数是初始化的函数。

主要算法思想是先序和后续的递归遍历(DFS)。

实现O(n2)的算法核心方程是:res[i] = res[root] - subc[i] + n - subc[i];

 

834. Sum of Distances in Tree —— weekly contest 84的更多相关文章

  1. [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 co ...

  2. [LeetCode] 834. Sum of Distances in Tree

    LeetCode刷题记录 传送门 Description An undirected, connected treewith N nodes labelled 0...N-1 and N-1 edge ...

  3. 【leetcode】834. Sum of Distances in Tree(图算法)

    There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are ...

  4. [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 co ...

  5. 树中的路径和 Sum of Distances in Tree

    2019-03-28 15:25:43 问题描述: 问题求解: 写过的最好的Hard题之一. 初看本题,很经典的路径和嘛,dfs一遍肯定可以得到某个节点到其他所有节点的距离和.这种算法的时间复杂度是O ...

  6. leetcode834 Sum of Distances in Tree

    思路: 树形dp. 实现: class Solution { public: void dfs(int root, int p, vector<vector<int>>& ...

  7. 835. Image Overlap —— weekly contest 84

    Image Overlap Two images A and B are given, represented as binary, square matrices of the same size. ...

  8. 833. Find And Replace in String —— weekly contest 84

    Find And Replace in String To some string S, we will perform some replacement operations that replac ...

  9. 832. Flipping an Image —— weekly contest 84

    Flipping an Image Given a binary matrix A, we want to flip the image horizontally, then invert it, a ...

随机推荐

  1. ATMEGA的SPI总线 - 第1部分

    转自: 1. https://www.yiboard.com/thread-782-1-1.html 2.https://mansfield-devine.com/speculatrix/2018/0 ...

  2. 怎么写一个Activity

    a.新建一个类继承Actitvity b.重写oncreate方法 setContentView(R.layout.XXX);//设置布局文件 c.注册activity <activity an ...

  3. 查杀进程小工具——WPF和MVVM初体验

    最近因为工作需要,研究了一下桌面应用程序.在winform.WPF.Electron等几种技术里,最终选择了WPF作为最后的选型.WPF最吸引我的地方,就是MVVM模式了.MVVM模式完全把界面和业务 ...

  4. Jmeter之『JSR223脚本』

    Json处理(通过JS) 对于Json字符串,需要使用单引号『''』(因为Json中已存在双引号) // String转为Object var jsonObj = JSON.parse('${data ...

  5. 小白安装使用Redis

    Redis属于NoSql中的键值数据库,非常适合海量数据读写. 之前用过mongo但是没有用过redis,今天来学习安装redis. 先去官网下载redis安装包 redis官网 redis是c语言编 ...

  6. MeteoInfoLab脚本示例:天气现象符号

    天气现象符号分布图实际就是散点图,可以用scatterm函数绘制,但之前需要创建天气符号图例,用weatherspec函数.如果只需要绘制某些天气现象(比如雾.霾),可以给出相应的天气符号序号列表(可 ...

  7. 发布MeteoInfo 1.2.4

    在JLaTeXMath库(http://forge.scilab.org/index.php/p/jlatexmath/)的支持下,实现了利用LaTeX语法显示特殊符号和数学公式的功能.需要在字符串首 ...

  8. Linux系统编程 —线程同步概念

    同步概念 同步,指对在一个系统中所发生的事件之间进行协调,在时间上出现一致性与统一化的现象. 但是,对于不同行业,对于同步的理解略有不同.比如:设备同步,是指在两个设备之间规定一个共同的时间参考:数据 ...

  9. 每个人都可以用C语言写的推箱子小游戏!今天你就可以写出属于自己项目~

    C语言,作为大多数人的第一门编程语言,重要性不言而喻,很多编程习惯,逻辑方式在此时就已经形成了.这个是我在大一学习 C语言 后写的推箱子小游戏,自己的逻辑能力得到了提升,在这里同大家分享这个推箱子小游 ...

  10. CSP-S2020AFO记

    2020-10.11 考初赛辣. 选择题考了一堆时间复杂度,一个不会(卒) 我寻思这01背包哪里能用贪心? 啊,这,这,这手写快排竟如此简单,手写取Max,手写队列,两个字符串颠来倒去,竟活到爆! 震 ...