leetcode834 Sum of Distances in Tree
思路:
树形dp。
实现:
class Solution
{
public:
void dfs(int root, int p, vector<vector<int>>& G, vector<int>& cnt, vector<int>& res)
{
for (auto it: G[root])
{
if (it == p) continue;
dfs(it, root, G, cnt, res);
cnt[root] += cnt[it];
res[root] += res[it] + cnt[it];
}
cnt[root]++;
}
void dfs2(int root, int p, vector<vector<int>>& G, vector<int>& cnt, vector<int>& res, int N)
{
for (auto it: G[root])
{
if (it == p) continue;
res[it] = res[root] - cnt[it] + N - cnt[it];
dfs2(it, root, G, cnt, res, N);
}
}
vector<int> sumOfDistancesInTree(int N, vector<vector<int>>& edges)
{
vector<vector<int>> G(N, vector<int>());
for (auto it: edges)
{
int a = it[], b = it[];
G[a].push_back(b); G[b].push_back(a);
}
vector<int> cnt(N, ), res(N, );
dfs(, -, G, cnt, res);
dfs2(, -, G, cnt, res, N);
return res;
}
}
leetcode834 Sum of Distances in Tree的更多相关文章
- 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 a ...
- [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 ...
- [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 ...
- 树中的路径和 Sum of Distances in Tree
2019-03-28 15:25:43 问题描述: 问题求解: 写过的最好的Hard题之一. 初看本题,很经典的路径和嘛,dfs一遍肯定可以得到某个节点到其他所有节点的距离和.这种算法的时间复杂度是O ...
- Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree andsum =
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- CodeChef Sum of distances(分治)
CodeChef Sum of distances(分治) 题目大意 有一排点,每个点 i 向 \(i + 1, i + 2, i + 3\) 分别连价值为 \(a_i,b_i,c_i\) 的有向边, ...
- 【leetcode】1161. Maximum Level Sum of a Binary Tree
题目如下: Given the root of a binary tree, the level of its root is 1, the level of its children is 2, a ...
随机推荐
- 32 | 为什么还有kill不掉的语句?
在 MySQL 中有两个 kill 命令:一个是 kill query + 线程 id,表示终止这个线程中正在执行的语句:一个是 kill connection + 线程 id,这里 connecti ...
- element---------------el-menu组件_实现路由跳转及当前项的设置
<el-menu router :default-active="$route.path" class="el-menu-vertical-demo" @ ...
- jsp利用webuploader实现超大文件分片上传、断点续传
1,项目调研 因为需要研究下断点上传的问题.找了很久终于找到一个比较好的项目. 在GoogleCode上面,代码弄下来超级不方便,还是配置hosts才好,把代码重新上传到了github上面. http ...
- learning scala type alise
How to use type alias to name a Tuple2 pair into a domain type called CartItem type CartItem[Donut, ...
- Linux swap的创建与配置
在Linux下,swap的作用类似Windows系统下的“虚拟内存”.当物理内存不足时,拿出部分硬盘空间当SWAP分区(虚拟成内存)使用,从而解决内存容量不足的情况.Linux下的swap有两种实现形 ...
- Java 8的Time包常用API
Date.Canlender.SimpleDateFormat类在新的Time包面前几乎没有优势 日期LocalDate,时间LocalTime,日期时间LocalDateTime. 时区ZoneId ...
- Hadoop hadoop的介绍和几种模式
Hadoop简介 Hadoop软件库是一个开源框架,允许使用简单的编程模型跨计算机集群分布式处理大型数据集.它旨在从单个服务器扩展到数千台计算机,每台计算机都提供本地计算和存储.库本身不是依靠硬件来提 ...
- Beta冲刺(2/4)
队名:福大帮 组长博客链接:https://www.cnblogs.com/mhq-mhq/p/11990570.html 作业博客 : https://edu.cnblogs.com/campus/ ...
- jQuery插件fontIconPicker配合FontAwesome字体图标库的使用
同样先上效果图: 怎么样,是不是很好看,jquery fontIconPicker这个插件做的很不错,支持分类,搜索,还有分页功能,可以自定义分页,具体的使用方法我就不一介绍了,我只说一下如何使用fo ...
- OpenJudge计算概论-奥运奖牌计数
/*===================================================================== 奥运奖牌计数 总时间限制: 1000ms 内存限制: 6 ...