python RE 求助

B3644 【模板】拓扑排序 / 家谱树

Caterpie_771 @ 2023-06-05 11:37:35

代码如下

dir = {}
n = int(input())
for i in range(n):
    dir[i + 1] = []
    m = input()
    for j in m:
        if j != " " and j != "0":
            dir[i + 1].append(int(j))
ans = []
while len(ans) < n:
    for i in dir.keys():
        if dir[i] == []:  # 出度为零
            dir[i] = -1  # 删除结点
            ans.append(str(i))
            continue
        if dir[i] == -1:  # 结点被删除
            continue
        num = 0
        for j in dir[i]:
            if dir[j] != -1:  # 出度不为零
                num = 1
                break
        if num == 0:  # 出度为零
            dir[i] = -1  # 删除结点
            ans.append(str(i))
print(" ".join(ans[::-1]))  # 逆序输出

除最后一个点,全RE了,不解


by Terrible @ 2023-06-05 12:33:27

@Caterpie_771 RE 原因:数据行尾有 \r,另外

    m = input()
    for j in m:
        if j != " " and j != "0":
            dir[i + 1].append(int(j))

你这个读入似乎是按照字符分解的吧,可以改成

    m = input().split()
    for j in m:
        dir[i + 1].append(int(j))

另外读入一行数据的格式是

l=list(map(int,input().split()))

by Caterpie_771 @ 2023-06-06 20:17:13

@Terrible 感谢大佬!


|