Problem p03026 (python)

Fixed submission:
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)
c = [int(i) for i in input().split()]
c.sort()
c.reverse()
ans_list = [0 for _ in range(n)]
ans = 0
p_list = [0]
ans_list[0] = c.pop(0)
while len(p_list) > 0:
    p = p_list.pop(0)
    for g in graph[p]:
        if ans_list[g] == 0:
            ans_list[g] = c.pop(0)
            ans += ans_list[g]
            p_list.append(g)
print(ans)
print(' '.join([str(i) for i in ans_list]))

Buggy submission (verdict: Runtime Error):
def dfs(u):
    color[u] = 1
    for v in graph[u]:
        if color[v] == 0:
            dfs(v)
    visit_list.append(u)
    ans_list.append(c[u])

visit_list = []
ans_list = []
n = int(input())
color = [0] * n
graph = [[] * n for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    graph[a].append(b)
    graph[b].append(a)    
c = [int(i) for i in input().split()]
c.sort(reverse=True)
for s in range(n):
    if color[s] == 0:
        dfs(s)
ans_list = list(reversed(ans_list))
print(sum(c[1:]))
print(" ".join([str(i) for i in ans_list]))

Buggy submission (verdict: Runtime Error):
def dfs(u):
    color[u] = 1
    for v in graph[u]:
        if color[v] == 0:
            dfs(v)
    visit_list.append(u)
    ans_list.append(c[u])

visit_list = []
ans_list = []
n = int(input())
color = [0] * n
graph = [[] * n for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    graph[a].append(b)
    graph[b].append(a)    
c = [int(i) for i in input().split()]
c.sort(reverse=True)
for s in range(n):
    if color[s] == 0:
        dfs(s)
ans = list(reversed(ans))
print(sum(c[1:]))
print(" ".join([str(i) for i in ans_list]))

Buggy submission (verdict: Runtime Error):
def dfs(u):
    color[u] = 1
    for v in graph[u]:
        if color[v] == 0:
            dfs(v)
    visit_list.append(u)
    ans_list.append(c[u])

visit_list = []
ans_list = []
n = int(input())
color = [0] * n
graph = [[] * n for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    graph[a].append(b)
    graph[b].append(a)    
c = [int(i) for i in input().split()]
c.sort(reverse=True)
for s in range(n):
    if color[s] == 0:
        dfs(s)
print(sum(c[1:]))
print(" ".join([str(i) for i in ans_list]))

Buggy submission (verdict: Runtime Error):
visit_list = []


def topological_sort():
    for s in range(n):
        if color[s] == 0:
            dfs(s)

def dfs(u):
    color[u] = 1
    for v in graph[u]:
        if color[v] == 0:
            dfs(v)
    visit_list.append(u)        
 
 
n = int(input())
color = [0] * n
graph = [[] * n for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    graph[a].append(b)
    graph[b].append(a)    

topological_sort()
c = [int(i) for i in input().split()]
c.sort()
t_list = [[visit_list[i], str(c[i])] for i in range(n)]
t_list.sort()
print(sum(c) - c[-1])
print(" ".join(list(map(lambda x: x[1], t_list))))

Buggy submission (verdict: Runtime Error):
visit_list = []


def topological_sort():
    for s in range(n):
        if color[s] == 0:
            dfs(s)

def dfs(u):
    color[u] = 1
    for v in graph[u]:
        if color[v] == 0:
            dfs(v)
    visit_list.append(u)        
 
 
n = int(input())
color = [0] * n
graph = [[] * n for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    graph[a].append(b)
    graph[b].append(a)    

topological_sort()
c = [int(i) for i in input().split()]
c.sort()
t_list = [[visit_list[i], c[i]] for i in range(n)]
t_list.sort()
print(sum(c[:-1]))
a_list = [str(t[1]) for t in t_list]
print(" ".join(a_list))

Buggy submission (verdict: Runtime Error):
visit_list = []
def topological_sort():
    for s in range(n):
        if color[s] == 0:
            dfs(s)

def dfs(u):
    color[u] = 1
    for v in graph[u]:
        if color[v] == 0:
            dfs(v)
    visit_list.append(u)        


n = int(input())
if n > 1:
    color = [0] * n
    graph = [[] * n for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(int, input().split())
        a, b = a - 1, b - 1
        graph[a].append(b)
        graph[b].append(a)    

    topological_sort()
    c = [int(i) for i in input().split()]
    c.sort()
    t_list = [[visit_list[i], c[i]] for i in range(n)]
    t_list.sort()
    print(sum(c[:-1]))
    a_list = [str(t[1]) for t in t_list]
    print(" ".join(a_list))
else:
    k = int(input())
    print(k)
    print(k)

Buggy submission (verdict: Runtime Error):

visit_list = []
def topological_sort():
    for s in range(n):
        if color[s] == 0:
            dfs(s)

def dfs(u):
    color[u] = 1
    for v in graph[u]:
        if color[v] == 0:
            dfs(v)
    visit_list.append(u)        


n = int(input())
color = [0] * n
graph = [[] * n for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    graph[a].append(b)
    graph[b].append(a)    

topological_sort()
c = [int(i) for i in input().split()]
c.sort()
t_list = [[visit_list[i], c[i]] for i in range(n)]
t_list.sort()
print(sum(c[:-1]))
a_list = [str(t[1]) for t in t_list]
print(" ".join(a_list))