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 ...
随机推荐
- 用集装箱装ASP。带有Docker和Azure Kubernetes服务的NET Core应用程序
介绍 曾经有一个单一软件应用程序的时代,整个应用程序被打包并部署在作为单个进程运行的单个服务器上.我们都知道,在这个模型中,单点故障可能会导致整个应用程序崩溃. 微服务体系结构的发展是为了解决单片应用 ...
- 十一长假我肝了这本超硬核PDF,现决定开源!!
写在前面 在 [冰河技术] 微信公众号中的[互联网工程]专题,更新了不少文章,有些读者反馈说,在公众号中刷 历史文章不太方便,有时会忘记自己看到哪一篇了,当打开一篇文章时,似乎之前已经看过了,但就是不 ...
- .NET Standard 类库的使用技巧
系列目录 [已更新最新开发文章,点击查看详细] 在前一篇博客<.NET Standard中配置TargetFrameworks输出多版本类库>中详细介绍了如何创建.配置.条件编译. ...
- Django的安装和项目的启动
一.安装(安装最新LTS版): 1.命令行安装 pip install django==1.11.18 -i 源 2.pycharm 安装 二.创建项目 1.命令行创建 下面的命令创建了一个名为 ...
- switch host 切换本地host
百度网盘提取地址 提取码: 753r 下载后放到软件目录即可使用
- Ubuntu安装zookeeper问题
在Ubuntu系统安装zookeeper后,启动报错: root@host8:/usr/solrcould/service1/zookeeper-3.5.0-alpha# sh bin/zkServe ...
- 在VMware虚拟机Ubuntu使用traceroute
Linux traceroute命令用于显示数据包到主机间的路径 traceroute指令让你追踪网络数据包的路由途径,预设数据包大小是40Bytes,用户可另行设置. Ubuntu命令行输入: 后面 ...
- .NET Core开源任务调度平台ScheduleMaster上新了
ScheduleMaster上一次比较大的更新还是在6月份,转眼已经快过去4个月了,这段时间比较忙,中间只更新过一次修复了几个小bug.要总结这次更新的话,必须要用"千呼万唤始出来" ...
- Linux系统及第三方应用官方文档
通过在线文档获取帮助 http://www.github.com https://www.kernel.org/doc/html/latest/ http://httpd.apache.org htt ...
- JavaWeb学习笔记(六)jsp
第六章.jsp 1.什么是jsp jsp:java server pages,java的服务器页面 作用:代替Servlet回传HTML页面的数据 因为Servlet程序回传HTML页面的数据很繁琐, ...