leetcode834 Sum of Distances in Tree
思路:
树形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的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- [LeetCode] 834. Sum of Distances in Tree
LeetCode刷题记录 传送门 Description An undirected, connected treewith N nodes labelled 0...N-1 and N-1 edge ...
- 【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 ...
- 树中的路径和 Sum of Distances in Tree
2019-03-28 15:25:43 问题描述: 问题求解: 写过的最好的Hard题之一. 初看本题,很经典的路径和嘛,dfs一遍肯定可以得到某个节点到其他所有节点的距离和.这种算法的时间复杂度是O ...
- 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 ...
- CodeChef Sum of distances(分治)
CodeChef Sum of distances(分治) 题目大意 有一排点,每个点 i 向 \(i + 1, i + 2, i + 3\) 分别连价值为 \(a_i,b_i,c_i\) 的有向边, ...
- 【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 ...
随机推荐
- LeetCode 246. Strobogrammatic Number
原题链接在这里:https://leetcode.com/problems/strobogrammatic-number/ 题目: A strobogrammatic number is a numb ...
- js中int和string数据类型互相转化实例
今天做项目的时候,碰到一个问题,需要把String类型的变量转化成int类型的.按照常规,我写了var i = Integer.parseInt("112");但控制台报错,说是“ ...
- 内存原理与PHP的执行过程
一.内存结构 栈区:保存的是变量名(术语:引用),对于cpu来说,读写速度很快 堆区:存储“复杂”的数据,数组.对象.字符串(字符串比较特殊)等 数据段:又分为数据段全局区(用于存储简单的数据,如数字 ...
- /dev/null和/dev/zero的作用
经常会看到dd命令用到/dev/zero文件,这里总结一下/dev/null和/dev/zero的作用和使用实例. 在类Unix系统(包括Linux)中,/dev/null 它是空设备,也称为位桶(b ...
- s-w-i-p-e-r做一个-老-唬-机-抽-蒋
<template> <div class="selfLotteryBox"> <div class="row"> < ...
- 用Python实现自己下载音乐的统计
今天看Python实例,学习了如何对文件进行操作,突然想把自己网易云音乐下载到本地的歌曲名单写到一个txt中,看看具体情况.当然,我现在肯定无法做到直接去网易云音乐上爬取,就做个最简单的吧,嘻嘻^-^ ...
- 1825:【01NOIP提高组】数的划分
#include<bits/stdc++.h> using namespace std; ],tot; void dfs(int num,int pos) { if(pos==k) ]) ...
- hive --metastore三种模式
在官网上对于这几种模式的介绍如下: 按Metastore数据库位置分: 1.本地/嵌入式Metastore数据库(Derby) 2.远程Metastore数据库(其他的关系型数据库,像mysql.or ...
- (转)glances用法
借鉴:https://www.ibm.com/developerworks/cn/linux/1304_caoyq_glances/index.html glances 可以为 Unix 和 Linu ...
- 在OpenFOAM中做用户自定义库——编译library【转载】
转载自:http://openfoam.blog.sohu.com/22041538.html OpenFOAM自己提供的标准类都是以库的形式提供的,并且利用头文件给出了库的应用接口.这样一来,用户的 ...