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

The ith edge connects nodes edges[i][0] and edges[i][1] together.

Return a list ans, where ans[i] is the sum of the distances between node i 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:
Here is a diagram of the given tree:
  0
 / 1  2
    /|  3 4 5
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.

Note: 1 <= N <= 10000

 1 class Solution {
 2 public:
 3     //相当于图来存来处理
 4     vector<vector<int>> tree;
 5     vector<int> res;
 6     vector<int> subc;
 7     int n;
 8     vector<int> sumOfDistancesInTree(int N, vector<vector<int>>& edges) {
 9         //tree.rvec(N,vector<int>(N));
10         //init
11         n = N;
12         tree.resize(N);            //初始化的函数
13         res.assign(N,0);
14         subc.assign(N,0);
15         //build tree
16         for(auto e : edges){               //遍历的技巧
17             tree[e[0]].push_back(e[1]);
18             tree[e[1]].push_back(e[0]);
19         }
20         set<int> visited1;
21         set<int> visited2;
22         DFS_POST(0,visited1);         //初始root任何值都行
23         DFS_PRE(0,visited2);
24         return res;
25
26     }
27     void DFS_POST(int root,set<int> &visited){            //传引用保存修改值
28         visited.insert(root);
29         for(auto i : tree[root]){
30             if(visited.find(i) == visited.end() ){
31                 DFS_POST(i,visited);
32                 subc[root] += subc[i];
33                 res[root] += res[i] + subc[i];
34             }
35         }
36         subc[root]++;  //加上自身节点
37     }
38     void DFS_PRE(int root,set<int> &visited){
39         visited.insert(root);
40         for(auto i : tree[root]){
41             if(visited.find(i) == visited.end()){
42                 res[i] = res[root] - subc[i] + n - subc[i];         //算法核心
43                 DFS_PRE(i,visited);
44             }
45         }
46     }
47
48 };

主要函数是初始化的函数。

主要算法思想是先序和后续的递归遍历(DFS)。

实现O(n2)的算法核心方程是:res[i] = res[root] - subc[i] + n - subc[i];

原文地址:https://www.cnblogs.com/jinjin-2018/p/9034094.html

时间: 2024-07-30 17:13:12

834. Sum of Distances in Tree —— weekly contest 84的相关文章

[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 connects nodes?edges[i][0]?and?edges[i][1]?together. Return a list?ans, where?ans[i]?is the sum of the distances between node?i?and all other nodes. Exa

[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 connects nodes edges[i][0]and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Exam

leetcode834 Sum of Distances in Tree

思路: 树形dp. 实现: 1 class Solution 2 { 3 public: 4 void dfs(int root, int p, vector<vector<int>>& G, vector<int>& cnt, vector<int>& res) 5 { 6 for (auto it: G[root]) 7 { 8 if (it == p) continue; 9 dfs(it, root, G, cnt, res)

835. Image Overlap —— weekly contest 84

Image Overlap Two images A and B are given, represented as binary, square matrices of the same size.  (A binary matrix has only 0s and 1s as values.) We translate one image however we choose (sliding it left, right, up, or down any number of units),

Given a tree, find the node with the minimum sum of distances to other nodes

O(n) complexity, have a traversal for the tree. Get the information of all children, then traverse the tree again. #include <iostream> #include <vector> #include <algorithm> #include <utility> using namespace std; class Node { publ

Leetcode Weekly Contest 86

Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 "幻方" 子矩阵?(每个子矩阵都是连续的). 直接模拟即可,本来是签到题,由于粗心,浪费了时间. 1 class Solution { 2 public: 3 int numMagicSquaresInside(vector&l

[Swift Weekly Contest 108]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

108th LeetCode Weekly Contest Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

108th LeetCode Weekly Contest Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t