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 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 ...
随机推荐
- 请编写sql多语句表值函数统,计指定年份中每本书的销售总额
create table 图书表( 书号 varchar(50), 书名 varchar(50), 单价 int ) create table 销售表( 书号 varchar(50), 销售时间 da ...
- 在sqlserver中创建表
1:在sql语句中,临时表有两类,分别是局部(local)和全局(global)临时表,局部临时表只在其会话(事务)中可见,全局临时表可以被会话(事务)中的任何程序或者 模块访问 2:创建局部临时表 ...
- 《VC++ 深入详解》 第3版 这是盗版书么~。。。
<VC++ 深入详解> 第3版 www.broadview.com.cn 书读到一小半,发现书重复了一部分,缺失一部分.... 难受~ 比较难继续下去了 有一样的小伙伴么~ <VC+ ...
- Recursive sequence (矩阵快速幂)2016ACM/ICPC亚洲区沈阳站
题目 Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recu ...
- Scala小记(一)
Scala小记----初识Scala 一,什么是Scale? Scala是一门面向对象的,使用JVM运行的函数式编程语言,(函数式编程语言:指的就是那些将方法或者说是函数来作为参数 进行传递的编程语言 ...
- win7如何安装maven
1.Maven的简介Maven是一个项目管理工具,主要用于Java平台的项目构建.依赖管理和项目生命周期管理. 当然对于我这样的程序猿来说,最大的好处就是对jar包的管理比较方便,只需要告诉Maven ...
- centos7.5安装gcc7.2.0
参考https://www.cnblogs.com/lazyInsects/p/9778910.html cd /usr/src wget https://mirrors.tuna.tsinghua. ...
- 关于android和Linux的一些问题
1.Android为什么选择java? 由于java虚拟机,实现软件层的编程与硬件无关性(无需进行特定编译或平台配置). 2.Android和Linux内核区别? Android上的应用软件运行在da ...
- 用网桥和veth实现容器的桥接模式
原理图如下 具体命令先不写了,有时间再写,主要还是用的上一篇说的知识.
- 正式班D8
2020.10.15星期四 正式班D8 一.上节课复习 OSI七层协议 socket socket是对传输层以下的封装 IP+port标识唯一一个基于网络通讯的软件 TCP与UDP TCP:因为在通信 ...