Problem p02850 (python)

Fixed submission:
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N-1)]
sortedAB = sorted(AB)
con = {i: [] for i in range(1, N+1)}
color = [0] * (N + 1)
for a, b in sortedAB:
    con[a].append(b)

for i in range(1, N + 1):
    now_c = 1
    for c in con[i]:
        if color[i] == now_c:
            now_c += 1
        color[c] = now_c
        now_c += 1

print(max(color))
for i in range(N-1):
    print(color[AB[i][1]])

a = 0

Buggy submission (verdict: Runtime Error):
from collections import deque
import sys

class Tree:
    def __init__(self, n, init_list, root=1):
        self.num = (n + 1)
        self.parents = [-1] * self.num
        self.first_child = [-1] * self.num
        self.next_child = [-1] * self.num
        self.check = [-1] * self.num
        self.g = {i: [] for i in range(1, self.num)}
        self.make_g(init_list, root)

    def make_g(self, con, root):
        m = len(con)
        for i in range(m):
            self.g[con[i][0]].append(con[i][1])
            self.g[con[i][1]].append(con[i][0])
        q = deque([root])
        while q != deque():
            node = q.pop()
            self.check[node] = 1
            cnt = 0
            for i, sub_node in enumerate(self.g[node]):
                if self.check[sub_node] != -1:
                    continue
                if cnt == 0:
                    self.first_child[node] = sub_node
                self.parents[sub_node] = node
                if i != len(self.g[node])-1:
                    self.next_child[sub_node] = self.g[node][i+1]
                q.append(sub_node)
                cnt += 1

    def childs(self, node):
        child_list = []
        if self.first_child[node] != -1:
            child_list.append(self.first_child[node])
            node = self.first_child[node]
            while 1:
                if self.next_child[node] == -1:
                    break
                else:
                    child_list.append(self.next_child[node])
                node = self.next_child[node]
        return child_list

    def parent(self, n):
        return self.parents[n]

input = sys.stdin.readline
N, Q = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N-1)]
P = [0] * Q
X = [0] * Q
V = [0] * (N + 1)
for i in range(Q):
    P, X = map(int, input().split())
    V[P] += X

tr = Tree(N, AB)

q = deque()
q.extend(tr.childs(1))
while q != deque():
    node = q.pop()
    V[node] += V[tr.parent(node)]
    if tr.childs(node):
        q.extend(tr.childs(node))

print(*V[1:])

Buggy submission (verdict: Runtime Error):
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N-1)]
con = [set() for _ in range(N+1)]

for ab in AB:
    con[ab[0]].add(ab[1])
    con[ab[1]].add(ab[0])

max_len = 0
for i, c in enumerate(con):
    if max_len <= len(c):
        max_len = len(c)
        max_ind = i

colors = [set() for i in range(N+1)]
all_colors = set([i for i in range(1, max_len+1)])
res = [0] * (N - 1)


for i, ab in enumerate(AB):
    res[i] = list(all_colors.difference(colors[ab[0]].union(colors[ab[1]])))[0]
    colors[ab[0]].add(res[i])
    colors[ab[1]].add(res[i])

print(max_len)
for r in res:
    print(r)

Buggy submission (verdict: Runtime Error):
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N-1)]
con = [set() for _ in range(N+1)]

for ab in AB:
    con[ab[0]].add(ab[1])
    con[ab[1]].add(ab[0])

max_len = 0
for i, c in enumerate(con):
    if max_len <= len(c):
        max_len = len(c)
        max_ind = i

colors = [set() for i in range(N+1)]
all_colors = set([i for i in range(1, max_len+1)])
res = [0] * (N - 1)


for i, ab in enumerate(AB):
    res[i] = list(all_colors.difference(colors[ab[0]].union(colors[ab[1]])))[0]
    colors[ab[0]].add(res[i])
    colors[ab[1]].add(res[i])

print(max_len)
for r in res:
    print(r)

Buggy submission (verdict: Runtime Error):
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N-1)]

max_len = 0
for i, c in enumerate(con):
    if max_len <= len(c):
        max_len = len(c)
        max_ind = i

colors = [set() for i in range(N+1)]
all_colors = set([i for i in range(1, max_len+1)])
res = [0] * (N - 1)


for i, ab in enumerate(AB):
    res[i] = list(all_colors.difference(colors[ab[0]].union(colors[ab[1]])))[0]
    colors[ab[0]].add(res[i])
    colors[ab[1]].add(res[i])

print(max_len)
for r in res:
    print(r)