当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- leetcode134. gas station
- Uniapp wechat applet monitoring network
- Image segmentation in opencv
- Lenovo hybrid cloud Lenovo xcloud: 4 major product lines +it service portal
- Explain Huawei's application market in detail, and gradually reduce 32-bit package applications and strategies in 2022
- You should use Google related products with caution
- Speaking of a software entrepreneurship project, is there anyone willing to invest?
- AVL balanced binary search tree
- [MySQL] detailed explanation of trigger content of database advanced
- Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
猜你喜欢

idea里使用module项目的一个bug

MySQL主从延迟的解决方案

oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量

How to realize sliding operation component in fast application

Implementation of navigation bar at the bottom of applet

Why choose cloud native database
![[Nanjing University] - [software analysis] course learning notes (I) -introduction](/img/57/bf652b36389d2bf95388d2eb4772a1.png)
[Nanjing University] - [software analysis] course learning notes (I) -introduction

联想混合云Lenovo xCloud:4大产品线+IT服务门户

Greenplum6.x监控软件搭建

Pointer advanced, string function
随机推荐
如何在图片的目标中添加目标的mask
测试人一定要会的技能:selenium的三种等待方式解读,清晰明了
Enterprise manager cannot connect to the database instance
[Yugong series] February 2022 U3D full stack class 008 - build a galaxy scene
Greenplum 6.x monitoring software setup
cmake命令行使用
详解华为应用市场2022年逐步减少32位包体上架应用和策略
Implement custom memory allocator
[Chongqing Guangdong education] accounting reference materials of Nanjing University of Information Engineering
How to integrate app linking services in harmonyos applications
平台化,强链补链的一个支点
阿里p8手把手教你,自动化测试应该如何实现多线程?赶紧码住
[Chongqing Guangdong education] organic electronics (Bilingual) reference materials of Nanjing University of Posts and Telecommunications
All about PDF crack, a complete solution to meet all your PDF needs
Analysis of abnormal channel number information before and after AGC re signature service
求有符号数的原码、反码和补码【C语言】
Three series of BOM elements
Redis fault handling "can't save in background: fork: cannot allocate memory“
Teach you how to select PCB board by hand (II)
Greenplum6.x-版本变化记录-常用手册