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 ...
随机推荐
- ThinkPHP 5 生命周期
前段时间用TP5开发了一个小程序,就熟悉了一下TP5.TP5是TP框架最新的一个版本,与以前的3还是有很大的区别,有人说和laravel比较靠近,其实也还好,每个人都有自己不同的看法,只要是选择一个自 ...
- C/C++的二分查找
假设有一种温度传感器,已经测得它的电压和温度的对应关系,将电压值以ADC转换后的数字量的值表示,形成温度-AD值的对照表,如下. 大致成一条反比关系的曲线. ADC的底层驱动已经写好,对外有一个接口可 ...
- ConcurrentHashMap原理分析(一)-综述
概述 ConcurrentHashMap,一个线程安全的高性能集合,存储结构和HashMap一样,都是采用数组进行分桶,之后再每个桶中挂一个链表,当链表长度大于8的时候转为红黑树,其实现线程安全的基本 ...
- Redis安装使用以及可能的算法实践
layout: post title: 2017-12-05-Redis 入门 tags: - Redis - 实时特征 - UCB, Tompson sampling Redis 是什么? Redi ...
- 多测师讲解python_模块(导入模块和内置模块)_高级讲师肖sir
#自定义模块# from aaa import * #指定导入某个包中具体的类.函数.方法## A.fun1(2,2) #import +模块名 :# # import +模块名+.+.+# # 导入 ...
- day29 Pyhton 面向对象 多态 封装
# coding:utf-8 # py2中的经典类 # class D:#没有继承object是经典类# pass # # def func(self): # # print('d') # class ...
- Jmeter请求之接口串联自动化测试(未完)
方案一:添加Cookie管理器,把用户的登录状态存在cookie管理器中,类似于浏览器 存储测试结果: 监听器->保存响应到文件,对结果进行存储 文件名前缀:保存到哪个地方前缀是什么D:\tes ...
- docker查看ip
docker查看容器的网络ip docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' conta ...
- linux(centos8):用systemctl管理war包形式的jenkins(java 14 / jenkins 2.257)
一,如何安装jenkins? 参见: https://www.cnblogs.com/architectforest/p/13685904.html 说明:刘宏缔的架构森林是一个专注架构的博客,地址: ...
- CentOS7防止root密码被破解
破解root密码 为了防止服务器被破坏,为了守护业务的和平,在服务器安全方面,首先我们要做到密码的安全.那么知道如何破解root密码才能让我们有针对性的防护.另外如果我们忘掉了root密码,也能知道如 ...