作者:Ms.lin
链接:
https://www.nowcoder.com/discuss/177870 来源:牛客网
逃生问题:
思路:根结点左右子树的最大结点数。本地测试通过,提交不通过,不知道哪错了,大佬们帮忙看看
import collections
import sys
n = int(input())
edges = []
while True:
a = []
s = input()
if s != "":
for x in s.split():
a.append(int(x))
edges.append(a)
else:
break
graph = collections.defaultdict(set)
for u, v in edges:
graph[u].add(v)
graph[v].add(u)
def dfs(graph, node, visited):
count = 1
visited[node] = True
for child in graph[node]:
if not visited[child]:
count += dfs(graph, child, visited)
return count
res = 0
visited = [False] * (n + 1)
visited[1] = True
for child in graph[1]:
nodeNums = dfs(graph, child, visited)
if res < nodeNums:
res = nodeNums
sys.stdout.write(str(res)+"\n")