题目是华为最近机考第二题,从下边博主的文章中可以找到
https://blog.csdn.net/qq_44998067/article/details/115980067
刚做的时候我读错题目了,以为是每个环两分,让你去数环的个数,直接把我难倒了,但是后来别人告诉我读错题了,很快啊,我使用深度优先搜索获得了题解,基于python3代码,仅供大家参考
复制代码
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
26
27
28
29
30
31
32
33
34# coding: utf-8 # 输入 M = int(input("")) # 模块总数 N = int(input("")) #后续N行 nodes = {} appear_points = set() for i in range(N): a,b = [int(i) for i in input("").split(" ")] if a not in nodes: nodes[a] = [b] else: nodes[a].append(b) appear_points.add(a) appear_points.add(b) # 函数 def dfs(num,start_num): if num not in nodes: # num没有下一个node return else: for next_num in nodes[num]: if next_num == start_num: return True result = dfs(next_num,start_num) if result: return True # main point = 10 point -= M - len(appear_points) for num in nodes.keys(): result = dfs(num,num) if result: point -= 2 break print(point)
说明:第一行M为模块总数;第二行为后续一共N行,后边为前者依赖后者,然后两个示例:
复制代码
1
2
3
4
5
65 3 0 1 1 2 2 0 结果:6
复制代码
1
2
3
4
5
6
7
86 5 0 1 0 2 1 3 2 4 2 5 结果:10
再说一句,我这里没有设置visited,因为后边的点永远不会再次到之前的点,除非有环
最后
以上就是健壮缘分最近收集整理的关于软件模块循环依赖题目题解(一)的全部内容,更多相关软件模块循环依赖题目题解(一)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复