Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
from collections import deque

def solve(N: int, a: "List[int]", b: "List[int]"):
    h = [[] for _ in range(N+1)]
    edges = [[-1]*(N+1) for _ in range(N+1)]
    
    ## 全てのNodeは親は一つだけ

    for i in range(N-1):
        h[a[i]].append((b[i],i))
        h[b[i]].append((a[i],i))
        edges[a[i]][b[i]] = i
        edges[b[i]][a[i]] = i

    queue = deque([1])

    ## 頂点iの親を格納
    parents = [-1]*(N+1)
    parents[1] = 0

    while queue:
        q = queue.pop()

        for child,_ in h[q]:
            if child != q and parents[child] == -1:
                parents[child] = q
                queue.append(child)

    answer = [0]*(N-1)
    max_color = max(len(a) for a in h)
    queue = deque([1])

    while queue:
        color = 1 
        q = queue.popleft()
        parent_edge = edges[parents[q]][q]
        ng_color = answer[parent_edge]
        
        for node, j in h[q]:
            ##もう埋まっているか、親と一緒
            if answer[j] > 0 or q == node:
                continue
            
            if color==ng_color:
                color += 1    

            answer[j] = color
            color+=1
            queue.append(node)

    print(max_color)
    for a in answer:
        print(a)

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, a, b)

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import sys
from collections import deque

def solve(N: int, a: "List[int]", b: "List[int]"):
    h = [[] for _ in range(N+1)]    

    for i in range(N-1):
        h[a[i]].append(b[i])
        h[b[i]].append(a[i])

    queue = deque([1])

    ## 頂点iの親を格納
    parents = [0]*(N+1)
    order = []

    while queue:
        q = queue.pop()
        order.append(q)

        for child in h[q]:
            if child == parents[q]:
                continue

            parents[child] = q
            queue.append(child)

    ## 頂点i と親nodeの間のcolor
    color = [0]*(N+1)

    for o in order:
        ng_color = color[o]
        cur_color = 1

        for node in h[o]:
            if node == parents[o]:
                continue

            if cur_color == ng_color:
                cur_color += 1
            
            color[node] = cur_color
            cur_color += 1


    answer = []
    append = answer.append
    for i in range(N-1):
        if parents[a[i]] == b[i]:
            append(color[a[i]])
        else:
            append(color[b[i]])

    print(max(answer))
    print(*answer, sep="\n")

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, a, b)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
from collections import deque

def solve(N: int, a: "List[int]", b: "List[int]"):
    h = [[] for _ in range(N+1)]
    edges = [[-1]*(N+1) for _ in range(N+1)]
    
    ## 全てのNodeは親は一つだけ

    for i in range(N-1):
        h[a[i]].append((b[i],i))
        h[b[i]].append((a[i],i))
    
    ## node二つ与えたときにその間の辺
    for i in range(N-1):
        edges[a[i]][b[i]] = i
        edges[b[i]][a[i]] = i

    queue = deque([1])

    ## 頂点iの親を格納
    parents = [-1]*(N+1)
    parents[1] = 0

    while queue:
        q = queue.pop()

        for child,_ in h[q]:
            if child != q and parents[child] == -1:
                parents[child] = q
                queue.append(child)

    answer = [0]*(N-1)
    max_color = max(len(a) for a in h)
    queue = deque([1])

    while queue:
        color = 1 
        q = queue.popleft()
        parent_edge = edges[parents[q]][q]
        ng_color = answer[parent_edge]
        
        for node, j in h[q]:
            ##もう埋まっているか、親と一緒
            if answer[j] > 0 or q == node:
                continue
            
            if color!=ng_color or q == 1:
                answer[j] = color
            else:
                color+=1
                answer[j] = color
            color+=1

            queue.append(node)

    print(max_color)
    for a in answer:
        print(a)

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, a, b)

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import sys
from collections import deque

def solve(N: int, a: "List[int]", b: "List[int]"):
    h = [[] for _ in range(N+1)]    

    for i in range(N-1):
        h[a[i]].append(b[i])
        h[b[i]].append(a[i])

    queue = deque([1])

    ## 頂点iの親を格納
    parents = [0]*(N+1)
    order = []

    while queue:
        q = queue.pop()
        order.append(q)

        for child in h[q]:
            if child == parents[q]:
                continue

            parents[child] = q
            queue.append(child)

    ## 頂点i と親nodeの間のcolor
    color = [0]*(N+1)

    for o in order:
        ng_color = color[o]
        cur_color = 1

        for node in h[o]:
            if node == parents[o]:
                continue

            if cur_color == ng_color:
                cur_color += 1
            
            color[node] = cur_color
            cur_color += 1


    answer = []
    append = answer.append
    for i in range(N-1):
        if parents[a[i]] == b[i]:
            append(color[a[i]])
        else:
            append(color[b[i]])

    print(max(answer))
    print(*answer, sep="\n")

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, a, b)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
from collections import deque

def solve(N: int, a: "List[int]", b: "List[int]"):
    h = [[] for _ in range(N+1)]
    edges = [[-1]*(N+1) for _ in range(N+1)]
    
    ## 全てのNodeは親は一つだけ

    for i in range(N-1):
        h[a[i]].append((b[i],i))
        h[b[i]].append((a[i],i))
    
    ## node二つ与えたときにその間の辺
    for i in range(N-1):
        edges[a[i]][b[i]] = i
        edges[b[i]][a[i]] = i

    queue = deque([1])

    ## 頂点iの親を格納
    parents = [-1]*(N+1)
    parents[1] = 0

    while queue:
        q = queue.pop()

        for child,_ in h[q]:
            if parents[child] == -1:
                parents[child] = q
                queue.append(child)


    answer = [0]*(N-1)
    max_color = max(len(a) for a in h)
    color = list(range(1,max_color+1))
    queue = deque([1])

    while queue:
        color_index = 0 
        q = queue.popleft()

        parent = parents[q]
        parent_edge = edges[parent][q]
        
        for node, j in h[q]:
            ##もう埋まっているか、親と一緒
            if answer[j] > 0 or q == node:
                continue
            
            if answer[parent_edge] != color[color_index] or q == 1:
                answer[j] = color[color_index]
            else:
                color_index+=1
                answer[j] = color[color_index]
            color_index+=1

            queue.append(node)


    print(max_color)
    for a in answer:
        print(a)

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, a, b)

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import sys
from collections import deque

def solve(N: int, a: "List[int]", b: "List[int]"):
    h = [[] for _ in range(N+1)]    

    for i in range(N-1):
        h[a[i]].append(b[i])
        h[b[i]].append(a[i])

    queue = deque([1])

    ## 頂点iの親を格納
    parents = [0]*(N+1)
    order = []

    while queue:
        q = queue.pop()
        order.append(q)

        for child in h[q]:
            if child == parents[q]:
                continue

            parents[child] = q
            queue.append(child)

    ## 頂点i と親nodeの間のcolor
    color = [0]*(N+1)

    for o in order:
        ng_color = color[o]
        cur_color = 1

        for node in h[o]:
            if node == parents[o]:
                continue

            if cur_color == ng_color:
                cur_color += 1
            
            color[node] = cur_color
            cur_color += 1


    answer = []
    append = answer.append
    for i in range(N-1):
        if parents[a[i]] == b[i]:
            append(color[a[i]])
        else:
            append(color[b[i]])

    print(max(answer))
    print(*answer, sep="\n")

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, a, b)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
from collections import deque

def solve(N: int, a: "List[int]", b: "List[int]"):
    h = [[] for _ in range(N+1)]
    edges = [[-1]*(N+1) for _ in range(N+1)]
    
    ## 全てのNodeは親は一つだけ

    for i in range(N-1):
        h[a[i]].append((b[i],i))
        h[b[i]].append((a[i],i))
    
    ## node二つ与えたときにその間の辺
    for i in range(N-1):
        edges[a[i]][b[i]] = i
        edges[b[i]][a[i]] = i

    queue = deque([1])

    ## 頂点iの親を格納
    parents = [-1]*(N+1)
    parents[1] = 0

    while queue:
        q = queue.pop()

        for child,_ in h[q]:
            if parents[child] == -1:
                parents[child] = q
                queue.append(child)


    answer = [0]*(N-1)
    max_color = max(len(a) for a in h)
    color = list(range(1,max_color+1))
    queue = deque([1])

    while queue:
        color_index = 0 
        q = queue.popleft()

        parent = parents[q]
        parent_edge = edges[parent][q]
        
        for node, j in h[q]:
            ##もう埋まっているか、親と一緒
            if answer[j] > 0 or q == node:
                continue
            
            if answer[parent_edge] != color[color_index] or q == 1:
                answer[j] = color[color_index]
            else:
                color_index+=1
                answer[j] = color[color_index]
            color_index+=1

            queue.append(node)


    print(max_color)
    for a in answer:
        print(a)

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, a, b)

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import sys
from collections import deque

def solve(N: int, a: "List[int]", b: "List[int]"):
    h = [[] for _ in range(N+1)]    

    for i in range(N-1):
        h[a[i]].append(b[i])
        h[b[i]].append(a[i])

    queue = deque([1])

    ## 頂点iの親を格納
    parents = [0]*(N+1)
    order = []

    while queue:
        q = queue.pop()
        order.append(q)

        for child in h[q]:
            if child == parents[q]:
                continue

            parents[child] = q
            queue.append(child)

    ## 頂点i と親nodeの間のcolor
    color = [0]*(N+1)

    for o in order:
        ng_color = color[o]
        cur_color = 1

        for node in h[o]:
            if node == parents[o]:
                continue

            if cur_color == ng_color:
                cur_color += 1
            
            color[node] = cur_color
            cur_color += 1


    answer = []
    append = answer.append
    for i in range(N-1):
        if parents[a[i]] == b[i]:
            append(color[a[i]])
        else:
            append(color[b[i]])

    print(max(answer))
    print(*answer, sep="\n")

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, a, b)

if __name__ == '__main__':
    main()