解题思路:
注意事项:
参考代码:
def find_root(tree):
# 找到根节点
nodes = set(tree.keys())
children = set()
for node in tree.values():
children.update(node)
return (nodes - children).pop()
def find_max_children(tree):
# 找到孩子最多的节点
max_node = None
max_count = 0
for node, children in tree.items():
if len(children) > max_count:
max_node = node
max_count = len(children)
return max_node
def find_children(tree, node):
# 找到节点的孩子
return sorted(tree[node])
# 读取输入
n, m = map(int, input().split()) # 结点个数和边数
tree = {}
for _ in range(m):
x, y = map(int, input().split()) # x是y的父节点
if x not in tree:
tree[x] = []
tree[x].append(y)
# 输出结果
root = find_root(tree)
max_node = find_max_children(tree)
children = find_children(tree, max_node)
print(root)
print(max_node)
print(" ".join(map(str, children)))
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题5.8 (C语言代码)浏览:798 |
输出正反三角形 (C语言代码)浏览:853 |
1231题解(注意理解“输入多个测试实例”)浏览:826 |
C语言程序设计教程(第三版)课后习题6.10 (C语言代码)浏览:535 |
老王赛马 (C++代码)浏览:962 |
C二级辅导-计负均正 (C语言代码)浏览:659 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:677 |
整数分类 oj上总是wrong answer浏览:681 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:578 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:547 |