思路:

树形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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. [LeetCode] 834. Sum of Distances in Tree

    LeetCode刷题记录 传送门 Description An undirected, connected treewith N nodes labelled 0...N-1 and N-1 edge ...

  5. 【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 ...

  6. 树中的路径和 Sum of Distances in Tree

    2019-03-28 15:25:43 问题描述: 问题求解: 写过的最好的Hard题之一. 初看本题,很经典的路径和嘛,dfs一遍肯定可以得到某个节点到其他所有节点的距离和.这种算法的时间复杂度是O ...

  7. 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 ...

  8. CodeChef Sum of distances(分治)

    CodeChef Sum of distances(分治) 题目大意 有一排点,每个点 i 向 \(i + 1, i + 2, i + 3\) 分别连价值为 \(a_i,b_i,c_i\) 的有向边, ...

  9. 【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 ...

随机推荐

  1. Spark mllib 随机森林算法的简单应用(附代码)

    此前用自己实现的随机森林算法,应用在titanic生还者预测的数据集上.事实上,有很多开源的算法包供我们使用.无论是本地的机器学习算法包sklearn 还是分布式的spark mllib,都是非常不错 ...

  2. hive on spark (spark2.0.0 hive2.3.3)

    hive on spark真的很折腾人啊!!!!!!! 一.软件准备阶段 maven3.3.9 spark2.0.0 hive2.3.3 hadoop2.7.6 二.下载源码spark2.0.0,编译 ...

  3. bootstrap-wysihtml5 ckeditor 修改富文本编辑器可以上传图片

    bootstrap-wysihtml5 ckeditor 修改富文本编辑器可以上传图片   bootstrap-wysihtml5实际使用内核为ckeditor     故这里修改ckeditor即可 ...

  4. RSA公钥私钥原理及作用

    RSA算法广泛应用与加密与认证两个领域 1.加密(保证数据安全性) 使用公钥加密,需使用私钥解密. 这种广泛应用在保证数据的安全性的方面,用户将自己的公钥广播出去,所有人给该用户发数据时使用该公钥加密 ...

  5. 三十五.MySQL读写分离 MySQL多实例 、MySQL性能调优

    1.实现MySQL读写分离 搭建一主一从结构 配置maxscale代理服务器 测试分离配置   1.1 搭建一主一从结构 192.168.4.51 主 192.168.4.52 从 测试OK   1. ...

  6. learning scala sealed class

    package com.aura.scala.day01 object sealedClassed { def findPlaceToSit(piece: Furniture) = piece mat ...

  7. 6、transformation和action1

    一.transformation和action入门 1.介绍 Spark支持两种RDD操作:transformation和action.transformation操作会针对已有的RDD创建一个新的R ...

  8. 一个类中域(field)的首字母不要大写

    首先这种写法不规范, 其次,至少在AJAX交互的情况下, 如果首字母大写,会无法与前端相同名称的JSON属性相绑定. 如 data:{'Name':'2017-10-19'} public NameI ...

  9. P5022 旅行

    原题链接  https://www.luogu.org/problem/P5022 本着快csp了,做点往年的NOIp的题试试水来着,没想到水这么深 难度还挺大的,耗了我一天的时间(可能是我太菜了) ...

  10. springboot连接redis进行CRUD

    springboot连接redis进行CRUD: 1.添加以下依赖: <dependency> <groupId>org.springframework.boot</gr ...