第五题不知哪里出问题,总是50%,不是时间复杂度和空间复杂度问题,总是出现语法错误和数组越界,是输入问题吗?求解 def dfs(item, temp_dict, result): if item in result: return result.add(item) for x in temp_dict[item]: dfs(x, temp_dict, result) if __name__ == "__main__": import sys N = int(sys.stdin.readline().strip()) M = int(sys.stdin.readline().strip()) if M > 0: line = sys.stdin.readline().strip() relation = list(map(int, line.split())) bucket = {} for i in range(0, 2 * M, 2): if relation[i + 1] not in bucket: bucket[relation[i + 1]] = [] bucket[relation[i + 1]].append(relation[i]) cnt = 0 for key in bucket.keys(): a = set() dfs(key, bucket, a) a.add(key) if len(a) == N: cnt += 1 print(cnt) else: print(N)
点赞 3

相关推荐

totoroyyw:千年老妖😂
投递华为等公司10个岗位
点赞 评论 收藏
分享
牛客网
牛客企业服务