Problem p02850 (python)

Fixed submission:
N = int(input())
AB = [[]]*(N-1)
for i in range(N-1):
    AB[i] = list(map(int,input().split()))
from collections import defaultdict as ddict
from collections import deque
tree = ddict(list)
B = [0]*(N-1)
for i,ab in enumerate(AB):
    tree[ab[0]].append(ab[1])
    B[i] = ab[1]
# 各エッジの色を格納する変数の初期化
# C[0]は使わない
C = [0]*N
# 幅優先探索用のcue
Q = deque([1])
while Q:
    q = Q.popleft()
    c = 0
    for w in tree[q]:
        # 色をインクリメントした際に親の色とかぶったら更に1を加算する
        # 他は単純なインクリメント
        c += 1 + int(C[q-1] == c+1)
        # エッジの色を格納
        C[w-1] = c
        # 子供をcueに追加
        Q.append(w)
print(max(C))
for b in B:
    print(C[b-1])

Buggy submission (verdict: Runtime Error):
N = int(input())
AB = []
for _ in range(N-1):
    AB.append([int(i) for i in input().split()])
from collections import defaultdict as ddict
import numpy as np
children = ddict(list)
parents = ddict(list)
for ab in AB:
    children[ab[0]].append(ab[1])
    parents[ab[1]].append(ab[0])
ans = []
ans_trans = ddict(int)
for i in range(N-1):
    used = set()
    # 親チェック
    if parents[AB[i][0]] != []:
        used.add(ans_trans[str(parents[AB[i][0]][0]) + "-" + str(AB[i][0])])
    # 子チェック
    for child in children[AB[i][1]]:
        used.add(ans_trans[str(AB[i][1])+"-"+str(child)])
    # 兄弟チェック
    for child in children[AB[i][0]]:
        used.add(ans_trans[str(AB[i][0])+"-"+str(child)])
    ans.append(min(set(range(1,max(used)+2)) - used))
    ans_trans[str(AB[i][0]) + "-" + str(AB[i][1])] = ans[-1]
print(max(ans))
for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
N = int(input())
AB = []
for _ in range(N-1):
    AB.append([int(i) for i in input().split()])
from collections import defaultdict as ddict
import numpy as np
R_children = ddict(set)
R_parents = ddict(set)
for ab in AB:
    R_children[ab[0]].add(ab[1])
    R_parents[ab[1]].add(ab[0])
ans = [0] * (N-1)
for i in range(N-1):
    used = set()
    for parent in R_parents[AB[i][0]]:
        color = ans[np.where(np.array(AB)[:,1]==AB[i][0])[0][0]]
        if color != 0:
            used.add(color) 
    for child in R_children[AB[i][1]]:
        color = ans[np.where(np.array(AB)[:,1]==child)[0][0]]
        if color != 0:
            used.add(color) 
    for child in R_children[AB[i][0]]:
        color = ans[np.where(np.array(AB)[:,1]==child)[0][0]]
        if color != 0:
            used.add(color) 
    if used == set():
        ans[i] = 1
    else:
        ans[i] = min(set(range(1,max(used)+2)) - used)
print(max(ans))
for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
N = int(input())
AB = []
for _ in range(N-1):
    AB.append([int(i) for i in input().split()])
ans = [0]*(N-1)
ans[0] = 0
for i,ab in enumerate(AB):
    if i==0:
        pass
    else:
        used = set()
        AB_temp = AB[0:i]
        for  j,ab_temp in enumerate(AB_temp):
            if ab_temp[0] == ab[0] or ab_temp[0] == ab[1] or ab_temp[1] == ab[0] or ab_temp[1] == ab[1]:
                used.add(ans[j])
        ans[i] = min(set(range(max(used) + 2)) - used)
print(max(ans)+1)
for a in ans:
    print(a+1)

Buggy submission (verdict: Runtime Error):
N = int(input())
AB = []
for _ in range(N-1):
    AB.append([int(i) for i in input().split()])
def used_col(N):
    col = set()
    # 子供の色
    for child in tree_children[N]:
        col.add(ans[child-2])
    # 親の色
    col.add(ans[tree_parent[N]-2])
    # 兄弟の色
    for child in tree_children[tree_parent[N]]:
        col.add(ans[child-2])
    return col
from collections import defaultdict as ddict
tree_children = ddict(set)
tree_parent = ddict(int)
for ab in AB:
    tree_children[ab[0]].add(ab[1])
    tree_parent[ab[1]] = ab[0]
max_col = 0
for key in tree_children.keys():
    parent_bool = 1 if tree_parent[key] != 0 else 0
    max_col = max(len(tree_children[key])+parent_bool , max_col)
ans = [0] * (N-1)
for i in range(2,N+1):
    available = set(range(1,max_col+1))
    NG_col = used_col(i)
    for NG in NG_col:
        if NG == 0:
            pass
        else:
            available.remove(NG)
    ans[i-2] = available[0]
print(max_col)
for a in ans:
    print(a)