解题思路:
注意事项:
参考代码:
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.7 (C语言代码)浏览:1199 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:681 |
C语言程序设计教程(第三版)课后习题9.2 (C语言代码)浏览:567 |
1124题解浏览:620 |
Tom数 (C语言代码)浏览:515 |
交换Easy (C语言代码)浏览:802 |
C语言程序设计教程(第三版)课后习题10.7 (用指针求解)浏览:1531 |
A+B for Input-Output Practice (I) (C语言代码)浏览:448 |
C二级辅导-温度转换 (C语言代码)浏览:570 |
简单的a+b (C语言代码)浏览:487 |