当前位置:网站首页>模拟卷Leetcode【普通】1557. 可以到达所有点的最少点数目
模拟卷Leetcode【普通】1557. 可以到达所有点的最少点数目
2022-07-07 06:18:00 【邂逅模拟卷】
1557. 可以到达所有点的最少点数目
给你一个 有向无环图 , n 个节点编号为 0 到 n-1 ,以及一个边数组 edges ,其中 edges[i] = [fromi, toi] 表示一条从点 fromi 到点 toi 的有向边。
找到最小的点集使得从这些点出发能到达图中所有点。题目保证解存在且唯一。
你可以以任意顺序返回这些节点编号。
示例 1:
输入:n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]]
输出:[0,3]
解释:从单个节点出发无法到达所有节点。从 0 出发我们可以到达 [0,1,2,5] 。从 3 出发我们可以到达 [3,4,2,5] 。所以我们输出 [0,3] 。
示例 2:
输入:n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]]
输出:[0,2,3]
解释:注意到节点 0,3 和 2 无法从其他节点到达,所以我们必须将它们包含在结果点集中,这些点都能到达节点 1 和 4 。
提示:
2 <= n <= 10^5
1 <= edges.length <= min(10^5, n * (n - 1) / 2)
edges[i].length == 2
0 <= fromi, toi < n
所有点对 (fromi, toi) 互不相同。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/minimum-number-of-vertices-to-reach-all-nodes
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
class Solution:
def __init__(self):
""" 超时case:https://leetcode-cn.com/submissions/detail/240988246/testcase/ """
pass
def findSmallestSetOfVertices(self, n: int, edges: List[List[int]]) -> List[int]:
res = set(range(n))
for start,end in edges:
if end in res: res.remove(end)
return list(res)
def findSmallestSetOfVertices_超时(self, n: int, edges: List[List[int]]) -> List[int]:
res = [x for x in range(n)]
for start,end in edges:
if end in res: res.remove(end)
return res
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list转node
return s.findSmallestSetOfVertices(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[6,[[0,1],[0,2],[2,5],[3,4],[4,2]]],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总_卷子的博客-CSDN博客
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Novice entry SCM must understand those things
- 阿里p8推荐,测试覆盖率工具—Jacoco,实用性极佳
- 南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案
- A bug using module project in idea
- [machine learning] watermelon book data set_ data sharing
- 【MySQL】数据库进阶之触发器内容详解
- 详解华为应用市场2022年逐步减少32位包体上架应用和策略
- [kuangbin] topic 15 digit DP
- Why choose cloud native database
- Appeler l'interface du moteur de création du service multimédia de jeu Huawei renvoie le Code d'erreur 1002, le message d'erreur: les paramètres sont l'erreur
猜你喜欢

Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error

Nanjing commercial housing sales enabled electronic contracts, and Junzi sign assisted in the online signing and filing of housing transactions

Input and output of floating point data (C language)

Novice entry SCM must understand those things

Image segmentation in opencv

All about PDF crack, a complete solution to meet all your PDF needs

Greenplum6.x搭建_安装

MySQL主从延迟的解决方案

Routing information protocol rip

Count sort (diagram)
随机推荐
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
Basic data types and string types are converted to each other
[Yu Yue education] C language programming reference of Zhongbei College of Nanjing Normal University
channel. Detailed explanation of queuedeclare parameters
Introduction to data fragmentation
Frequently Asked Coding Problems
JS operation
Data type - floating point (C language)
Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
Why choose cloud native database
Sign and authenticate API interface or H5 interface
opencv 将16位图像数据转为8位、8转16
最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
数字三角形模型 AcWing 1027. 方格取数
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
LeetCode 736. Lisp 语法解析
LeetCode 715. Range 模块
Rapid integration of authentication services - harmonyos platform