Problem p03026 (python)

Fixed submission:
from collections import deque

class Node:
    def __init__(self):
        self.val = -1
        self.link = []

def main():
    N = int(input())
    nodes = [Node() for i in range(N)]
    for i in range(N-1):
        a, b = map(int, input().split())
        a -= 1; b -= 1;
        nodes[a].link.append(nodes[b])
        nodes[b].link.append(nodes[a])
    c = list(map(int, input().split()))
    c = sorted(c, reverse=True)
    count = 0
    q = deque()
    q.append(nodes[0])
    while len(q) > 0:
        node = q.popleft()
        node.val = c[count]
        count += 1
        for next in node.link:
            if next.val == -1:
                q.append(next)
    print(sum(c) - c[0])
    for i in range(N):
        print(nodes[i].val, end="")
        if i != N-1:
            print(" ",end="")
        else:
            print()

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
class Node:
    def __init__(self, i):
        self.index = i
        self.val = -1
        self.links = 0
        self.nodes = []

def depth_search(node):
    if node.val == -1:
        node.val = c.pop(0)
        for child in node.nodes:
            if child != node:
                depth_search(child)

N = int(input())

nodes = [Node(i) for i in range(N)]
count = [0 for i in range(N)]

for i in range(N-1):
    a, b = map(int, input().split())
    node_a = nodes[a-1]
    node_b = nodes[b-1]
    node_b.nodes.append(node_a)
    node_a.links += 1
    node_a.nodes.append(node_b)
    node_b.links += 1

c = list(map(int, input().split()))
c = sorted(c, reverse=True)
max_c = max(c)

print(sum(c) - max_c)

depth_search(nodes[0])

for i in range(N-1):
    print(nodes[i].val, end=" ")
print(nodes[-1].val)

Buggy submission (verdict: Runtime Error):
class Node:
    def __init__(self, i):
        self.index = i
        self.val = -1
        self.nodes = []
        self.link = 0

    def __lt__(self, other):
        # self < other
        return self.link < other.link

def width_search(node):
    if node.val == -1:
        node.val = c.pop(0)
        for child in sorted(node.nodes):
            width_search(child)

N = int(input())
if N == 1:
    c = list(map(int, input().split()))
    print(0)
    print(c[0])
else:
    nodes = [Node(i) for i in range(N)]

    for i in range(N-1):
        a, b = map(int, input().split())
        node_a = nodes[a-1]
        node_b = nodes[b-1]
        node_b.nodes.append(node_a)
        node_b.link += 1
        node_a.nodes.append(node_b)
        node_a.link += 1

    c = list(map(int, input().split()))
    c = sorted(c, reverse=True)

    max_c = 0
    sum_c = 0
    for tmp in c:
        sum_c += tmp
        max_c = max(max_c, tmp)

    width_search(max(nodes))

    print(sum_c - max_c)
    for i in range(N-1):
        print(nodes[i].val, end=" ")
    print(nodes[-1].val)

Buggy submission (verdict: Runtime Error):
class Node:
    def __init__(self, i):
        self.index = i
        self.val = -1
        self.nodes = []
        self.link = 0

    def __lt__(self, other):
        # self < other
        return self.link < other.link

def width_search(node):
    if node.val == -1:
        node.val = c.pop(0)
        for child in sorted(node.nodes):
            width_search(child)

N = int(input())
if N == 1:
    c = int(input())
    print(0)
    print(c)
else:
    nodes = [Node(i) for i in range(N)]

    for i in range(N-1):
        a, b = map(int, input().split())
        node_a = nodes[a-1]
        node_b = nodes[b-1]
        node_b.nodes.append(node_a)
        node_b.link += 1
        node_a.nodes.append(node_b)
        node_a.link += 1

    c = list(map(int, input().split()))
    c = sorted(c, reverse=True)

    max_c = 0
    sum_c = 0
    for tmp in c:
        sum_c += tmp
        max_c = max(max_c, tmp)

    width_search(max(nodes))

    print(sum_c - max_c)
    for i in range(N-1):
        print(nodes[i].val, end=" ")
    print(nodes[-1].val)

Buggy submission (verdict: Runtime Error):
class Node:
    def __init__(self, i):
        self.index = i
        self.val = -1
        self.nodes = []
        self.link = 0

    def __lt__(self, other):
        # self < other
        return self.link < other.link

def width_search(node):
    if node.val == -1:
        node.val = c.pop(0)
        for child in sorted(node.nodes):
            width_search(child)

N = int(input())
if N == 1:
    print(0)
else:
    nodes = [Node(i) for i in range(N)]

    for i in range(N-1):
        a, b = map(int, input().split())
        node_a = nodes[a-1]
        node_b = nodes[b-1]
        node_b.nodes.append(node_a)
        node_b.link += 1
        node_a.nodes.append(node_b)
        node_a.link += 1

    c = list(map(int, input().split()))
    c = sorted(c, reverse=True)

    max_c = 0
    sum_c = 0
    for tmp in c:
        sum_c += tmp
        max_c = max(max_c, tmp)

    width_search(max(nodes))

    print(sum_c - max_c)
    for i in range(N-1):
        print(nodes[i].val, end=" ")
    print(nodes[-1].val)

Buggy submission (verdict: Runtime Error):
class Node:
    def __init__(self, i):
        self.index = i
        self.val = -1
        self.nodes = []
        self.link = 0

    def __lt__(self, other):
        # self < other
        return self.link < other.link

def width_search(node):
    if node.val == -1:
        node.val = c.pop(0)
        for child in sorted(node.nodes):
            width_search(child)

N = int(input())

nodes = [Node(i) for i in range(N)]

for i in range(N-1):
    a, b = map(int, input().split())
    node_a = nodes[a-1]
    node_b = nodes[b-1]
    if node_a not in node_b.nodes:
        node_b.nodes.append(node_a)
        node_b.link += 1
    if node_b not in node_a.nodes:
        node_a.nodes.append(node_b)
        node_a.link += 1

c = list(map(int, input().split()))
c = sorted(c, reverse=True)

max_c = 0
sum_c = 0
for tmp in c:
    sum_c += tmp
    max_c = max(max_c, tmp)

width_search(max(nodes))

print(sum_c - max_c)
for i in range(N-1):
    print(nodes[i].val, end=" ")
print(nodes[-1].val)