Problem p02850 (python)

Fixed submission:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(100000)

def main():
    N = int(input())
    sides = [[] for _ in range(N-1)]
    
    for i in range(N-1):
        a,b = map(int, input().split())
        sides[a-1].append((b-1,i))

    ans = [[None] for _ in range(N-1)]

    def dfs(focus,parent_color):
        color = 1
        for child,index in sides[focus]:
            if parent_color == color:
                color += 1 #もし親の色と被っていた場合はその色を飛ばす
            ans[index] = color
            if child < N-1:
                dfs(child,color)
            color += 1 #同じ親から続く次の辺の色を変更する

    dfs(0,0)

    print(max(ans))
    for color in ans:
        print(color)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

# def main():
#     N = int(input())
#     sides = [[] for _ in range(N-1)]
    
#     for i in range(N-1):
#         a,b = map(int, input().split())
#         sides[a-1].append((b-1,i))

#     ans = [[None] for _ in range(N-1)]

#     def dfs(focus,parent_color):
#         color = 1
#         for child,index in sides[focus]:
#             if parent_color == color:
#                 color += 1 #もし親の色と被っていた場合はその色を飛ばす
#             ans[index] = color
#             if child < N-1:
#                 dfs(child,color)
#             color += 1 #同じ親から続く次の辺の色を変更する

#     dfs(0,0)

#     print(max(ans))
#     for color in ans:
#         print(color)

def main():
    N = int(input())

    g = [[] for _ in range(N-1)]
    for i in range(N-1):
        a, b = map(int, input().split())
        g[a-1].append((b-1, i))

    ans = [[None] for _ in range(N-1)]

    def dfs(v0, c0):
        c = 1
        for v1, e in g[v0]:
            if c==c0:
                c += 1
            ans[e] = c
            if v1 < N-1:
                dfs(v1, c)
            c += 1

    dfs(0, 0)

    print(max(ans))
    for c in ans:
        print(c)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import copy

def main():
    N = int(input())
    sides = [[] for _ in range(N-1)]
    
    for i in range(N-1):
        a,b = map(int, input().split())
        sides[a-1].append((b-1,i))

    ans = [[None] for _ in range(N-1)]

    def dfs(focus,parent_color):
        color = 1
        for child,index in sides[focus]:
            if parent_color == color:
                color += 1 #もし親の色と被っていた場合はその色を飛ばす
            ans[index] = color
            if child < N-1:
                dfs(child,color)
            color += 1 #同じ親から続く次の辺の色を変更する

    dfs(0,0)

    print(max(ans))
    for color in ans:
        print(color)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import copy

def main():
    N = int(input())
    sides = [[] for _ in range(N-1)]
    
    for i in range(N-1):
        a,b = map(int, input().split())
        sides[a-1].append((b-1,i))

    ans = [[None] for _ in range(N-1)]

    def dfs(focus,parent_color):
        # if focus == N - 1:
        #     return
        color = 1
        for child,index in sides[focus]:
            if parent_color == color:
                color += 1 #もし親の色と被っていた場合はその色を飛ばす
            ans[index] = color
            if child < N -1:
                dfs(child,color)
            color += 1 #同じ親から続く次の辺の色を変更する

    dfs(0,0)

    print(max(ans))
    for color in ans:
        print(color)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import copy

def main():
    N = int(input())
    sides = [[] for _ in range(N-1)]
    
    for i in range(N-1):
        a,b = map(int, input().split())
        sides[a-1].append((b-1,i))

    ans = [[None] for _ in range(N-1)]

    def dfs(focus,parent_color):
        if focus == N - 1:
            return
        color = 1
        for child,index in sides[focus]:
            if parent_color == color:
                color += 1 #もし親の色と被っていた場合はその色を飛ばす
            ans[index] = color
            dfs(child,color)
            color += 1 #同じ親から続く次の辺の色を変更する

    dfs(0,0)

    print(max(ans))
    for color in ans:
        print(color)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import copy

def main():
    N = int(input())
    sides = [[] for _ in range(N-1)]
    
    for i in range(N-1):
        a,b = map(int, input().split())
        sides[a-1].append((b-1,i))

    ans = [[None] for _ in range(N-1)]

    def dfs(focus,parent_color):
        if focus == N - 1:
            return
        color = 1
        for child,index in sides[focus]:
            if parent_color == color:
                color += 1 #もし親の色と被っていた場合はその色を飛ばす
            ans[index] = color
            dfs(child,color)
            color += 1

    dfs(0,0)

    print(max(ans))
    for color in ans:
        print(color)

def main_ver0():
    N = int(input())
    sides = [[int(s) for s in input().split()] for i in range(N-1)]

    # まず最大次数を探す
    sides = sorted(sides)
    max_degree = 0
    current = 1
    current_degree = 0
    for side in sides:
        if side[0]==current:
            current_degree += 1
        else:
            current = side[0]
            current_degree = 2 #見ている頂点が変わるということは、1つ前の頂点から必ず1つ辺が出ているため
        max_degree = max(current_degree,max_degree)
    
    # 上から順番見ていくと、既に使われている色はひとつだけなので、それを除いて塗っていくだけでいい
    # 使用可能な色リスト
    color_list = [i for i in range(1,max_degree+1)]
    color_lists = []
    for i in range(N):
        color_lists.append(copy.copy(color_list))

    sides_color = []
    current = 0
    for side in sides:
        choose_color = color_lists[side[0]-1].pop(0)
        sides_color.append(choose_color)
        color_lists[side[1]-1].remove(choose_color) #次の頂点が使用可能な色から使用済の色を除く

    print(max_degree)
    for color in sides_color:
        print(color)

if __name__ == "__main__":
    main()