There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are given the integer n and the array edges where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. Return an array answer of length n where answer[i] is the sum of the distances between the ith node in the tree 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: The tree is shown above. 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.
 
  

class Solution {
public:
vector<int> sumOfDistancesInTree(int n, vector<vector<int>>& edges) {
//好久没做和图论相关的题目了 图论的题目 dfs bfs?
//链接图链表
vector<int> res(n),count(n);
vector<vector<int>> tree(n);
for(auto &edge:edges)
{
tree[edge[0]].push_back(edge[1]);
tree[edge[1]].push_back(edge[0]); }
helper(tree,0,-1,count,res);
helper2(tree,0,-1,count,res);
return res; } void helper(vector<vector<int>> &tree,int cur,int pre,vector<int>&count,vector<int>& res)
{
for(int i:tree[cur])
{
if(i==pre) continue;
helper(tree,i,cur,count,res);
count[cur]+=count[i];//这个统计更新的逻辑 还是不懂。。
res[cur]+=res[i]+count[i];
}
++count[cur];
}
void helper2(vector<vector<int>> &tree,int cur,int pre,vector<int>&count,vector<int>& res)
{
for(int i:tree[cur])
{
if(i==pre) continue;
res[i]=res[cur]-count[i]+count.size()-count[i];
helper2(tree, i, cur, count, res);
}
}
};

【leetcode】834. Sum of Distances in Tree(图算法)的更多相关文章

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

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

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

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

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

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

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

  6. leetcode834 Sum of Distances in Tree

    思路: 树形dp. 实现: class Solution { public: void dfs(int root, int p, vector<vector<int>>& ...

  7. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

  8. [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

    Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...

  9. [LeetCode] Path Sum 二叉树的路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

随机推荐

  1. es date_histogram强制补零

    es补零 GET /cars/transactions/_search { "size" : 0, "aggs": { "sales": { ...

  2. [第二章]c++学习笔记5(类型转换构造函数)

    使用例 析构函数 使用例 析构函数和数组 delete运算符导致析构函数的调用

  3. 菜鸡的Java笔记 - java 正则表达式

    正则表达式 RegularExpression        了解正则表达式的好处        正则表达式的基础语法        正则表达式的具体操作            content (内容 ...

  4. 菜鸡的Java笔记 comparator 比较器

    1.数组操作类: Arrays        2.两种比较器的使用: Comparable Comparator        3.实现二叉树算法            content (内容)   ...

  5. Mybatis动态传入tableName--非预编译(STATEMENT)

    在使用Mybatis过程中,你可以体会到它的强大与灵活之处,由衷的为Mybatis之父点上999个赞!在使用过程中经常会遇到这样一种情况,我查询数据的时候,表名称是动态的从程序中传入的,比如我们通过m ...

  6. 关于【【故障公告】数据库服务器 CPU 近 100% 引发的故障(源于 .NET Core 3.0 的一个 bug)】IS NOT NULL测试

    测试如图,Core_Users的PhoneNumber可为空,存在索引,记录数1500000+ 增加is not null,查询计划消耗增加了一个0%的筛选器消耗,IO消耗如下一模一样 如果是IS N ...

  7. [cf461E]Appleman and a Game

    考虑我的每一次添加操作,要满足:1.该串是t的子串:2.该串不能与下一次的串开头字母构成t的子串.那么,设f[i][j][k]表示拼i次,第i次填入的开头字母是j,第i+1填入的开头字母是k的最短长度 ...

  8. [atARC115D]Odd Degree

    考虑对于一棵树$G$,这个问题的答案-- 当$k$为奇数时答案显然为0,否则从$V$中任选$k$个点,以任意一点为根,从底往上不难发现子图数量唯一 换言之,当$k$为偶数时,每一个合法(恰有$k$个奇 ...

  9. [bzoj1106]立方体大作战

    先贪心,容易发现如果两个点中间没有点对,那么一定可以先把这两个点消掉分析一下,就可以发现这样两个点的答案就是这两个点对中间不成对的点数量扫描过去,线段树维护每一个点的权值(是否会被算入答案)即可 1 ...

  10. rocketmq 精华

    (ps:)通过本人语雀文档阅读体验更好哦--有目录 介绍 rocket mq 翻译成中文就是火箭消息队列,从名字就可以看出来,它是一个很快的消息队列... rocket mq 是 阿里巴巴研制的后面贡 ...