LeetCode 题解工作台

在带权树网络中统计可连接服务器对数目

给你一棵无根带权树,树中总共有 n 个节点,分别表示 n 个服务器,服务器从 0 到 n - 1 编号。同时给你一个数组 edges ,其中 edges[i] = [a i , b i , weight i ] 表示节点 a i 和 b i 之间有一条双向边,边的权值为 weight i 。再给你一…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 二分·树·traversal

bolt

答案摘要

我们先根据题目给定的边构建出一个邻接表 ,其中 表示节点 的所有邻居节点以及对应的边权。 然后,我们可以枚举每一个节点 作为连接的中间节点,通过深度优先搜索计算出从 的邻居节点 出发的,且到节点 的距离可以被 整除的节点数 。那么,节点 的可连接节点对数目增加了 $s \times t$,其中 表示节点 的邻居节点 出发的,且到节点 的距离不可以被 整除的累计节点数。然后…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 二分·树·traversal 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一棵无根带权树,树中总共有 n 个节点,分别表示 n 个服务器,服务器从 0 到 n - 1 编号。同时给你一个数组 edges ,其中 edges[i] = [ai, bi, weighti] 表示节点 ai 和 bi 之间有一条双向边,边的权值为 weighti 。再给你一个整数 signalSpeed 。

如果两台服务器 a 和 b 是通过服务器 c 可连接的,则:

  • a < b ,a != c 且 b != c 。
  • 从 c 到 a 的距离是可以被 signalSpeed 整除的。
  • 从 c 到 b 的距离是可以被 signalSpeed 整除的。
  • 从 c 到 b 的路径与从 c 到 a 的路径没有任何公共边。

请你返回一个长度为 n 的整数数组 count ,其中 count[i] 表示通过服务器 i 可连接 的服务器对的 数目 。

 

示例 1:

输入:edges = [[0,1,1],[1,2,5],[2,3,13],[3,4,9],[4,5,2]], signalSpeed = 1
输出:[0,4,6,6,4,0]
解释:由于 signalSpeed 等于 1 ,count[c] 等于所有从 c 开始且没有公共边的路径对数目。
在输入图中,count[c] 等于服务器 c 左边服务器数目乘以右边服务器数目。

示例 2:

输入:edges = [[0,6,3],[6,5,3],[0,3,1],[3,2,7],[3,1,6],[3,4,2]], signalSpeed = 3
输出:[2,0,0,0,0,0,2]
解释:通过服务器 0 ,有 2 个可连接服务器对(4, 5) 和 (4, 6) 。
通过服务器 6 ,有 2 个可连接服务器对 (4, 5) 和 (0, 5) 。
所有服务器对都必须通过服务器 0 或 6 才可连接,所以其他服务器对应的可连接服务器对数目都为 0 。

 

提示:

  • 2 <= n <= 1000
  • edges.length == n - 1
  • edges[i].length == 3
  • 0 <= ai, bi < n
  • edges[i] = [ai, bi, weighti]
  • 1 <= weighti <= 106
  • 1 <= signalSpeed <= 106
  • 输入保证 edges 构成一棵合法的树。
lightbulb

解题思路

方法一:枚举 + DFS

我们先根据题目给定的边构建出一个邻接表 gg,其中 g[a]g[a] 表示节点 aa 的所有邻居节点以及对应的边权。

然后,我们可以枚举每一个节点 aa 作为连接的中间节点,通过深度优先搜索计算出从 aa 的邻居节点 bb 出发的,且到节点 aa 的距离可以被 signalSpeedsignalSpeed 整除的节点数 tt。那么,节点 aa 的可连接节点对数目增加了 s×ts \times t,其中 ss 表示节点 aa 的邻居节点 bb 出发的,且到节点 aa 的距离不可以被 signalSpeedsignalSpeed 整除的累计节点数。然后我们更新 sss+ts + t

枚举完所有节点 aa 之后,我们就可以得到所有节点的可连接节点对数目。

时间复杂度 O(n2)O(n^2),空间复杂度 O(n)O(n)。其中 nn 表示节点数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
    def countPairsOfConnectableServers(
        self, edges: List[List[int]], signalSpeed: int
    ) -> List[int]:
        def dfs(a: int, fa: int, ws: int) -> int:
            cnt = 0 if ws % signalSpeed else 1
            for b, w in g[a]:
                if b != fa:
                    cnt += dfs(b, a, ws + w)
            return cnt

        n = len(edges) + 1
        g = [[] for _ in range(n)]
        for a, b, w in edges:
            g[a].append((b, w))
            g[b].append((a, w))
        ans = [0] * n
        for a in range(n):
            s = 0
            for b, w in g[a]:
                t = dfs(b, a, w)
                ans[a] += s * t
                s += t
        return ans
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for a solution that efficiently calculates connectable pairs using DFS traversal and subtree size tracking.

  • question_mark

    Candidates should demonstrate how they optimize the pair-counting process to avoid checking all possible pairs.

  • question_mark

    Be aware of the need to consider the signalSpeed constraint when connecting servers through intermediate nodes.

warning

常见陷阱

外企场景
  • error

    Forgetting to account for the signalSpeed when determining connectable pairs through each server.

  • error

    Inefficient pair counting that leads to excessive computation, especially for larger inputs.

  • error

    Not properly managing subtree size and distance calculations during the DFS, resulting in incorrect pair counts.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Handling cases where signalSpeed is very large or small compared to the tree's structure.

  • arrow_right_alt

    Exploring alternative methods for DFS traversal that avoid redundant calculations for each server.

  • arrow_right_alt

    Optimizing space complexity by reducing the need to store all distances or subtree sizes at once.

help

常见问题

外企场景

在带权树网络中统计可连接服务器对数目题解:二分·树·traversal | LeetCode #3067 中等