解题思路:
注意事项:
参考代码:
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语言代码)浏览:672 |
C语言程序设计教程(第三版)课后习题6.11 (C语言代码)浏览:550 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:674 |
WU-C语言程序设计教程(第三版)课后习题11.11 (C++代码)(想学链表的可以看看)浏览:1381 |
Hello, world! (C语言代码)浏览:725 |
C语言程序设计教程(第三版)课后习题10.3 (C语言代码)浏览:1944 |
青年歌手大奖赛_评委会打分 (C语言代码)浏览:2177 |
C语言程序设计教程(第三版)课后习题6.5 (C语言代码)浏览:540 |
1073题解浏览:626 |
简单的a+b (C语言代码)浏览:479 |