思路:

树形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. Mysql存储过程查询数据插入别的表里。

    DELIMITER// CREATE PROCEDURE setRoomManger2() BEGIN ); ; DECLARE cur CURSOR FOR SELECT roomid FROM n ...

  2. 072_查看所有虚拟机磁盘使用量以及 CPU 使用量信息

    #!/bin/bashvirt-df #虚拟机磁盘使用量read -n1 "按任意键继续" keyvirt-top # CPU 使用量

  3. 强大的捉包工具Fiddler

    Fiddler2出现 creation of the root certificate was not successful 错误 http://www.zhaokeli.com/Article/63 ...

  4. git 代码回滚与爬坑 -- reset and revert

    本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/git_code_roll_back_revert_and_res ...

  5. Hadoop Aggregate Resource Allocation解释

    1.在hadoop里面运行程序的时候,查看某个任务的具体信息如下: [hadoop@master monitor]$ yarn application -list 如上图,这里面的Aggregate ...

  6. PHP 之查找字符串位置函数封装

    /** * 正数查找字符串n次出现的位置 * @param $str * @param $find * @param $n * @return bool|int */ function str_n_p ...

  7. Why use swap when there is more than enough RAM.

    Swappiness is a property of the Linux kernel that changes the balance between swapping out runtime m ...

  8. BZOJ1941Hide and Seek

    做KD_tree的入门题. 问题就是求出任意一个点距其他点的最大曼哈顿距离和最小曼哈顿距离差,然后对其取min即可. 这个东西就是KD_tree可以轻松解决的了. 下面总结一下做KD_tree(不带修 ...

  9. linux下 安装 ImageMagick 及其 php imagick扩展(转)

    linux下 安装 ImageMagick 及其 php imagick扩展 PHP版本7.1.3 : ImageMagick版本 ImageMagick-7.0.8-3: PHP扩展imagick版 ...

  10. Cesium的Property机制总结

    前言 Cesium官方教程中有一篇叫<空间数据可视化>(Visualizing Spatial Data).该文文末简单提到了Cesium的Property机制,然后话锋一转,宣告此教程的 ...