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 i
th 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:
Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]]
Output: [8,12,6,10,10,10]
Explanation:
Here is a diagram of the given tree:
0
/ \
1 2
/|\
3 4 5
We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5)
equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on.
Note: 1 <= N <= 10000
1 class Solution {
2 public:
3 //相当于图来存来处理
4 vector<vector<int>> tree;
5 vector<int> res;
6 vector<int> subc;
7 int n;
8 vector<int> sumOfDistancesInTree(int N, vector<vector<int>>& edges) {
9 //tree.rvec(N,vector<int>(N));
10 //init
11 n = N;
12 tree.resize(N); //初始化的函数
13 res.assign(N,0);
14 subc.assign(N,0);
15 //build tree
16 for(auto e : edges){ //遍历的技巧
17 tree[e[0]].push_back(e[1]);
18 tree[e[1]].push_back(e[0]);
19 }
20 set<int> visited1;
21 set<int> visited2;
22 DFS_POST(0,visited1); //初始root任何值都行
23 DFS_PRE(0,visited2);
24 return res;
25
26 }
27 void DFS_POST(int root,set<int> &visited){ //传引用保存修改值
28 visited.insert(root);
29 for(auto i : tree[root]){
30 if(visited.find(i) == visited.end() ){
31 DFS_POST(i,visited);
32 subc[root] += subc[i];
33 res[root] += res[i] + subc[i];
34 }
35 }
36 subc[root]++; //加上自身节点
37 }
38 void DFS_PRE(int root,set<int> &visited){
39 visited.insert(root);
40 for(auto i : tree[root]){
41 if(visited.find(i) == visited.end()){
42 res[i] = res[root] - subc[i] + n - subc[i]; //算法核心
43 DFS_PRE(i,visited);
44 }
45 }
46 }
47
48 };
主要函数是初始化的函数。
主要算法思想是先序和后续的递归遍历(DFS)。
实现O(n2)的算法核心方程是:res[i] = res[root] - subc[i] + n - subc[i];
834. Sum of Distances in Tree —— weekly contest 84的更多相关文章
- [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 ...
- [LeetCode] 834. Sum of Distances in Tree
LeetCode刷题记录 传送门 Description An undirected, connected treewith N nodes labelled 0...N-1 and N-1 edge ...
- 【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 ...
- [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 ...
- 树中的路径和 Sum of Distances in Tree
2019-03-28 15:25:43 问题描述: 问题求解: 写过的最好的Hard题之一. 初看本题,很经典的路径和嘛,dfs一遍肯定可以得到某个节点到其他所有节点的距离和.这种算法的时间复杂度是O ...
- leetcode834 Sum of Distances in Tree
思路: 树形dp. 实现: class Solution { public: void dfs(int root, int p, vector<vector<int>>& ...
- 835. Image Overlap —— weekly contest 84
Image Overlap Two images A and B are given, represented as binary, square matrices of the same size. ...
- 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 ...
- 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 ...
随机推荐
- .Net Core 2.2 存取Cookie
第一步(注释代码):注释Startup.cs中 ConfigureServices 函数中的 options.CheckConsentNeeded = context => true; 第二步 ...
- C++系列教程
C++系列教程: 本人是一个高二狗C++小白,之前徘徊在Python和易语言等一些语言之间,这是我几天学习收获的结果,该教程是我自己搜集整理,再加上自己对C++的理解编写的,也是一个偏经验类型的,希望 ...
- 初识HTTP 图解 形象生动
使用软件: 通信猫 请自行下载 使用平台: win7 360浏览器 当前PC连接的是无线网,所以第2步查看的是 无线局域网适配器上的IP地址
- [VBA原创源代码] excelhome 汇总多工作表花名册
生病了,一点一滴的积累,慢慢康复,今年十月,我就 2 周岁了. 以下代码完成了excelhome中留的作业 http://club.excelhome.net/forum.php?mod=viewth ...
- Arduino 跑马灯
参考: 1. https://blog.csdn.net/hunhun1122/article/details/70254606 2. http://www.51hei.com/arduino/392 ...
- Matlab2016b安装流程
来源:https://jingyan.baidu.com/article/59703552da12ab8fc007402b.html Matlab2016b安装教程 听语音 原创 | 浏览:34338 ...
- 《C++primerplus》第12章练习题
做一下倒数两题,都是在队列模拟的程序基础上做点修改测试. 5.找出平均等候时间为1分钟时,每小时到达的客户数为多少(试验时间不少于100小时). 指定队伍最大长度10人,模拟100小时.粗略估计答案在 ...
- Python初学习:简单的练习题
Python初学习 一些见到那的练习题: 初级难度 设计一重量转换器,输入以g为单位的数字后,返回换算结果以Kg为单位的结果 中级难度 设计一个求直角三角形斜边长的函数,(以两个直角边为参数,求最长边 ...
- 多测师_测试理轮_002(v模型和H模型)
为什么要测试?1.软件非正常运行或自身缺陷会引发问题2.代码和文档是人写的,难免会出错3.环境原因影响软件(内存不足,存储,数据库溢出等)4.软件测试活动是保证软件质量的关键之一 什么是测试?软件行业 ...
- MeteoInfoLab脚本示例:获取气团轨迹每个节点的气象数据
读取HYSPLIT输出的轨迹数据文件和相应时间的气象数据文件,生成轨迹图层,循环每条轨迹的节点,读出该节点的经度.纬度.气压.时间,通过对气象数据插值获得该节点的气象数据.脚本程序: #------- ...