思路:

树形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. 34、[源码]-AOP原理-链式调用通知方法

    34.[源码]-AOP原理-链式调用通知方法

  2. ZooInspector使用

    一.工具 ZooInspector作用: 可以利用该工具图形化浏览ZK中的文件及文件夹 下载地址: https://issues.apache.org/jira/secure/attachment/1 ...

  3. 学到了林海峰,武沛齐讲的Day19 迭代细讲

    在家加1个月学了8day的课  出差6天看了8day的课..说明再忙也是可以挤挤多学习的. 广州出差最后两天没学习.一天做车,一天做公司的事...4天就过去了. 老师讲的包子和鸡蛋需求不好...讲的有 ...

  4. 网络OSI和TCP/IP参考模型详解

    网络模型 对应协议 转载自: https://www.2cto.com/kf/201612/576253.html

  5. 062_判断用户输入的是 Yes 或 NO

    #!/bin/bashread -p "Are you sure?[y/n]:" surecase $sure iny|Y|Yes|YES)     echo "you ...

  6. The database principal owns a schema in the database, and cannot be dropped. (.Net SqlClient Data Pr

    解决microsoft sql server error:15138的方法 http://blog.csdn.net/gray13/article/details/4458523 用sp_change ...

  7. notepad++修改背景色

  8. Ubuntu 14.04 indigo 相关依赖

    sudo apt-get install libbullet-dev sudo apt-get install ros-indigo-bfl sudo apt-get install libsdl-d ...

  9. codeforces#1249F. Maximum Weight Subset(树上dp)

    题目链接: http://codeforces.com/contest/1249/problem/F 题意: 一棵树的每个节点有个权值,选择一个节点集,使得任意点对的距离大于$k$ 求最大节点集权值, ...

  10. Nexus 3搭建及备份恢复

    Nexus 3搭建 官网下载相应的软件版本:Nexus官网 配置仓库存放地址 # tar xf xxxx # more bin/nexus.vmoptions -Xms500M -Xmx500M -X ...