Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(50000)

tree = {}
N = 0

def assign(assignment, u, prev_color):
    color = 1
    for v in tree[u]:
        if color == prev_color:
            color += 1
        assignment[u,v] = color
        assign(assignment, v, color)
        color += 1

def main():
    global tree, N
    N = int(input())
    tree = {i: [] for i in range(1,N+1)}
    edges = []
    for _ in range(N-1):
        a, b = map(int, input().split())
        tree[a].append(b)
        edges.append((a, b))
    ans = {}
    assign(ans, 1, None)
    print(max(ans.values()))
    for edge in edges:
        print(ans[edge])

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

tree = {}
N = 0

def assign(assignment, u, prev_color):
    color = 1
    for v in tree[u]:
        if color == prev_color:
            color += 1
        assignment[u,v] = color
        assign(assignment, v, color)
        color += 1

def main():
    global tree, N
    N = int(input())
    tree = {i: [] for i in range(1,N+1)}
    edges = []
    for _ in range(N-1):
        a, b = map(int, input().split())
        tree[a].append(b)
        edges.append((a, b))
    ans = {}
    assign(ans, 1, None)
    print(max(ans.values()))
    for edge in edges:
        print(ans[edge])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(50000)

tree = {}
colors = set()
N = 0

def assign(assignment, u, prev_color):
    rest_colors = colors - {prev_color}
    for v, color in zip(tree[u], rest_colors):
        assignment[u,v] = color
        assign(assignment, v, color)

def main():
    global tree, colors, N
    N = int(input())
    colors = set(range(1, N))
    tree = {i: [] for i in range(1,N+1)}
    edges = []
    for _ in range(N-1):
        a, b = map(int, input().split())
        tree[a].append(b)
        edges.append((a, b))
    ans = {}
    assign(ans, 1, None)
    print(max(ans.values()))
    for edge in edges:
        print(ans[edge])

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

tree = {}
N = 0

def assign(assignment, u, prev_color):
    color = 1
    for v in tree[u]:
        if color == prev_color:
            color += 1
        assignment[u,v] = color
        assign(assignment, v, color)
        color += 1

def main():
    global tree, N
    N = int(input())
    tree = {i: [] for i in range(1,N+1)}
    edges = []
    for _ in range(N-1):
        a, b = map(int, input().split())
        tree[a].append(b)
        edges.append((a, b))
    ans = {}
    assign(ans, 1, None)
    print(max(ans.values()))
    for edge in edges:
        print(ans[edge])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(50000)

tree = {}
edges = []
colors = set()
N = 0

def readlines(n):
    for _ in range(n):
        a, b = map(int, input().split())
        yield a, b

def to_tree(edges_, n):
    tree_ = {i:[] for i in range(1,n+1)}
    for a, b in edges_:
        tree_[a].append(b)
    return tree_

def assign(assignment, u, prev_color):
    lest_colors = colors - {prev_color}
    for v, color in zip(tree[u], lest_colors):
        assignment[u,v] = color
        assign(assignment, v, color)

def main():
    global tree, edges, colors, N
    N = int(input())
    colors = set(range(1,N))
    edges = tuple(readlines(N-1))
    tree = to_tree(edges, N)
    ans = {}
    assign(ans, 1, None)
    print(max(ans.values()))
    for edge in edges:
        print(ans[edge])

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

tree = {}
N = 0

def assign(assignment, u, prev_color):
    color = 1
    for v in tree[u]:
        if color == prev_color:
            color += 1
        assignment[u,v] = color
        assign(assignment, v, color)
        color += 1

def main():
    global tree, N
    N = int(input())
    tree = {i: [] for i in range(1,N+1)}
    edges = []
    for _ in range(N-1):
        a, b = map(int, input().split())
        tree[a].append(b)
        edges.append((a, b))
    ans = {}
    assign(ans, 1, None)
    print(max(ans.values()))
    for edge in edges:
        print(ans[edge])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(50000)

tree = {}
edges = []
colors = set()
N = 0

def readlines(n):
    for _ in range(n):
        a, b = map(int, input().split())
        yield a, b

def to_tree(edges_, n):
    tree_ = {i:[] for i in range(1,n+1)}
    for a, b in edges_:
        tree_[a].append(b)
        tree_[b].append(a)
    return tree_

def suc(u, parent):
    for v in [v for v in tree[u] if (u,v) in edges and (u,v) != (u, parent)]:
        yield u, v

def assign(assignment, u, parent):
    suc_edges = suc(u, parent)
    if not suc_edges:
        return

    lest_colors = colors - {assignment.get((parent, u))}
    for edge, color in zip(suc_edges, lest_colors):
        assignment[edge] = color
        v = edge[0] if edge[1] == u else edge[1]
        assign(assignment, v, u)

def main():
    global tree, edges, colors, N
    N = int(input())
    colors = set(range(1,N))
    edges = list(readlines(N-1))
    tree = to_tree(edges, N)
    ans = {}
    assign(ans, 1, None)
    print(max(ans.values()))
    for edge in edges:
        print(ans[edge])

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

tree = {}
N = 0

def assign(assignment, u, prev_color):
    color = 1
    for v in tree[u]:
        if color == prev_color:
            color += 1
        assignment[u,v] = color
        assign(assignment, v, color)
        color += 1

def main():
    global tree, N
    N = int(input())
    tree = {i: [] for i in range(1,N+1)}
    edges = []
    for _ in range(N-1):
        a, b = map(int, input().split())
        tree[a].append(b)
        edges.append((a, b))
    ans = {}
    assign(ans, 1, None)
    print(max(ans.values()))
    for edge in edges:
        print(ans[edge])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(50000)

tree = {}
edges = []
colors = set()
N = 0

def readlines(n):
    for _ in range(n):
        a, b = map(int, input().split())
        yield a, b

def to_tree(edges_, n):
    tree_ = {i:[] for i in range(1,n+1)}
    for a, b in edges_:
        tree_[a].append(b)
        tree_[b].append(a)
    return tree_

def suc(u, parent):
    for v in tree[u]:
        if (u, v) != (u, parent):
            yield u, v

def assign(assignment, u, parent):
    suc_edges = suc(u, parent)
    if not suc_edges:
        return

    lest_colors = colors - {assignment.get((parent, u))}
    for edge, color in zip(suc_edges, lest_colors):
        assignment[edge] = color
        v = edge[0] if edge[1] == u else edge[1]
        assign(assignment, v, u)

def main():
    global tree, edges, colors, N
    N = int(input())
    colors = set(range(1,N))
    edges = list(readlines(N-1))
    tree = to_tree(edges, N)
    ans = {}
    assign(ans, 1, None)
    print(max(ans.values()))
    for edge in edges:
        print(ans[edge])

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

tree = {}
N = 0

def assign(assignment, u, prev_color):
    color = 1
    for v in tree[u]:
        if color == prev_color:
            color += 1
        assignment[u,v] = color
        assign(assignment, v, color)
        color += 1

def main():
    global tree, N
    N = int(input())
    tree = {i: [] for i in range(1,N+1)}
    edges = []
    for _ in range(N-1):
        a, b = map(int, input().split())
        tree[a].append(b)
        edges.append((a, b))
    ans = {}
    assign(ans, 1, None)
    print(max(ans.values()))
    for edge in edges:
        print(ans[edge])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(500000)

N = 0
colors = []

def readlines(n):
    for _ in range(n):
        a, b = map(int, input().split())
        yield a, b

def used_colors(assignment, u, v):
    return {color for e, color in assignment.items() if e[0] in (u, v) or e[1] in (u, v)}

def assign(assignment, edges):
    if not edges:
        return assignment
    
    local_assignment = assignment.copy()
    colored = used_colors(local_assignment, *edges[0])
    local_assignment[edges[0]] = [color for color in colors if color not in colored][0]
    return assign(local_assignment, edges[1:] if len(edges) > 1 else [])

def main():
    global N, colors
    N = int(input())
    colors = list(range(1,N+1))
    edges = list(readlines(N-1))
    ans = assign({}, edges)
    print(max(ans.values()))
    for e in edges:
        print(ans[e])

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

tree = {}
N = 0

def assign(assignment, u, prev_color):
    color = 1
    for v in tree[u]:
        if color == prev_color:
            color += 1
        assignment[u,v] = color
        assign(assignment, v, color)
        color += 1

def main():
    global tree, N
    N = int(input())
    tree = {i: [] for i in range(1,N+1)}
    edges = []
    for _ in range(N-1):
        a, b = map(int, input().split())
        tree[a].append(b)
        edges.append((a, b))
    ans = {}
    assign(ans, 1, None)
    print(max(ans.values()))
    for edge in edges:
        print(ans[edge])

if __name__ == "__main__":
    main()