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. HTML基础-3

    图像标签(<img>)和源属性(Src) 在 HTML 中,图像由 <img> 标签定义. <img> 是空标签,意思是说,它只包含属性,并且没有闭合标签. 要在页 ...

  2. sql注入理解

    一.SQL注入产生的原因和危害 1.原因 SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序.而这些输入大都是SQL语法里的一些组合,通过执行SQL语句进而执行攻击者所要的操作,其主要原 ...

  3. Excel 读写

    一.环境准备:pom.xml 导入依赖 poi-ooxml <dependencies> <dependency> <groupId>org.apache.poi& ...

  4. springcloud zuul shiro网关鉴权并向服务传递用户信息

    1.pom文件 <dependencies> <!--eureka客户端--> <dependency> <groupId>org.springfram ...

  5. .net core api 请求实现接口幂等性

    简单实现接口幂等性,根据参数的hascode实现: 参数介绍  WaitMillisecond : 请求等待毫秒数 CacheMillisecond:请求结果缓存毫秒数 参数具体使用场景 WaitMi ...

  6. 南京大学OS笔记(1)-应用眼中的操作系统

    南京大学OS笔记(1)-应用眼中的操作系统 早就想刷一刷南大JYY老师的os课.之前稍微看过几节,果然讲的风趣幽默,而且现场写代码展示水平确实很高,这次准备认真刷一刷然后好好记一下笔记.当然lab就不 ...

  7. Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()

    在Java中处理JSON格式的数据时,Google  Gson 是个不错的选择,用起来挺方便的,也有一定灵活性.我现在工作中在参与的两个项目里都有用它.不过它在处理Date格式时有个小陷阱,在不同环境 ...

  8. 暑假算法练习Day1

    为了加强个人的算法能力,所以准备每天都做适当的算法练习,并在隔天加以回顾. 依托PTA.Leetcode平台进行训练(暂定每天三题C++,对于Leetcode平台上的练习,由于其解题需以类的形式进行提 ...

  9. [loj2504]小H爱染色

    以下考虑直接对所有$F(A)$求和,并给出两种做法-- 做法1: 枚举答案$A$,对应方案数为${n-A\choose m}^{2}-{n-A-1\choose m}^{2}$,即答案为$\sum_{ ...

  10. [atAGC051C]Flipper

    对于这一个平面用$a_{x,y}$来表示,即$(x,y)$为黑色则$a_{x,y}=1$,否则$a_{x,y}=0$,之后定义$a$能生成$b$当且仅当$a$能够通过若干次操作后得到$b$ 令$p_{ ...