Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
input = sys.stdin.readline
    
def bfs(v, N, G, E):
    visited = [0]*N
    queue = deque()
    new_color = 1
    used_color = set()
    num_used_color = 0
    node2colors = [set() for _ in range(N)]
    queue.append(v)
    visited[v] = 1
    while queue:
        q = queue.popleft()
        # print("q",q)
        num_color_from_q = 0
        while G[q]:
            nex = G[q].popleft()
            if visited[nex]:
                continue
            visited[nex] = 1
            rest = num_used_color - num_color_from_q
            # print(rest)
            if rest > 0:
                usd_c = used_color - node2colors[q]
                if usd_c:
                    # print(used_color, node2colors[q])
                    usd_c = min(usd_c)
                    E[(q, nex)][1] = usd_c
                    node2colors[q].add(usd_c)
                    num_color_from_q += 1
                    node2colors[nex].add(usd_c)
                else:
                    # print(num_used_color, num_color_from_q)
                    new_c = new_color
                    new_color += 1
                    num_used_color += 1
                    E[(q, nex)][1] = new_c
                    node2colors[q].add(new_c)
                    num_color_from_q += 1
                    node2colors[nex].add(new_c)
                    used_color.add(new_c)
                    
            else:
                new_c = new_color
                new_color += 1
                num_used_color += 1
                E[(q, nex)][1] = new_c
                node2colors[q].add(new_c)
                num_color_from_q += 1
                node2colors[nex].add(new_c)
                used_color.add(new_c)
        
            queue.append(nex)
    return num_used_color

def main():
    N = int(input())
    G = [deque() for _ in range(N)]
    E = dict() # (a, b) -> [idx, color]
    for i in range(N-1):
        a, b = map(lambda x: int(x)-1, input().split())
        G[a].append(b)
        G[b].append(a)
        E[(a, b)] = [i, 0]
    K = bfs(0, N, G, E)
    # ans_list = [value for value in E.values()]
    print(K)
    for value in E.values():
        print(value[1])
main()

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline
    
def bfs(v, N, G, E):
    visited = [0]*N
    queue = deque()
    K = -1
    node2color = [-1 for _ in range(N)]
    queue.append(v)
    visited[v] = 1
    while queue:
        q = queue.popleft()
        color = 0
        for nex in G[q]:
            if visited[nex]:
                continue
            visited[nex] = 1
            color += 1
            if color == node2color[q]:
                color += 1
            
            node2color[nex] = color
            E[(min(q, nex), max(q, nex))] = color
            queue.append(nex)
        K = max(K, color)
    return K

def main():
    N = int(input())
    G = [deque() for _ in range(N)]
    E = dict() # (a, b) -> [idx, color]
    for i in range(N-1):
        a, b = map(lambda x: int(x)-1, input().split())
        G[a].append(b)
        G[b].append(a)
        E[(a, b)] = 0
    K = bfs(0, N, G, E)
    # ans_list = [value for value in E.values()]
    print(K)
    for value in E.values():
        print(value)
main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
input = sys.stdin.readline
    
def bfs(v, N, G, E):
    queue = deque()
    new_color = 1
    used_color = set()
    num_used_color = 0
    node2colors = [set() for _ in range(N)]
    queue.append(v)
    while queue:
        q = queue.popleft()
        # print("q",q)
        num_color_from_q = 0
        while G[q]:
            nex = G[q].popleft()
            rest = num_used_color - num_color_from_q
            # print(rest)
            if rest > 0:
                usd_c = used_color - node2colors[q]
                if usd_c:
                    # print(used_color, node2colors[q])
                    usd_c = min(usd_c)
                    E[(q, nex)][1] = usd_c
                    node2colors[q].add(usd_c)
                    num_color_from_q += 1
                    node2colors[nex].add(usd_c)
                else:
                    # print(num_used_color, num_color_from_q)
                    new_c = new_color
                    new_color += 1
                    num_used_color += 1
                    E[(q, nex)][1] = new_c
                    node2colors[q].add(new_c)
                    num_color_from_q += 1
                    node2colors[nex].add(new_c)
                    used_color.add(new_c)
                    
            else:
                new_c = new_color
                new_color += 1
                num_used_color += 1
                E[(q, nex)][1] = new_c
                node2colors[q].add(new_c)
                num_color_from_q += 1
                node2colors[nex].add(new_c)
                used_color.add(new_c)
        
            queue.append(nex)
    return num_used_color

def main():
    N = int(input())
    G = [deque() for _ in range(N)]
    E = dict() # (a, b) -> [idx, color]
    for i in range(N-1):
        a, b = map(lambda x: int(x)-1, input().split())
        G[a].append(b)
        E[(a, b)] = [i, 0]
    K = bfs(0, N, G, E)
    # ans_list = [value for value in E.values()]
    print(K)
    for value in E.values():
        print(value[1])
main()

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline
    
def bfs(v, N, G, E):
    visited = [0]*N
    queue = deque()
    K = -1
    node2color = [-1 for _ in range(N)]
    queue.append(v)
    visited[v] = 1
    while queue:
        q = queue.popleft()
        color = 0
        for nex in G[q]:
            if visited[nex]:
                continue
            visited[nex] = 1
            color += 1
            if color == node2color[q]:
                color += 1
            
            node2color[nex] = color
            E[(min(q, nex), max(q, nex))] = color
            queue.append(nex)
        K = max(K, color)
    return K

def main():
    N = int(input())
    G = [deque() for _ in range(N)]
    E = dict() # (a, b) -> [idx, color]
    for i in range(N-1):
        a, b = map(lambda x: int(x)-1, input().split())
        G[a].append(b)
        G[b].append(a)
        E[(a, b)] = 0
    K = bfs(0, N, G, E)
    # ans_list = [value for value in E.values()]
    print(K)
    for value in E.values():
        print(value)
main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
input = sys.stdin.readline
    
def bfs(v, N, G, E):
    K = 0
    queue = deque()
    new_color = 1
    used_color = set()
    node2colors = [set() for _ in range(N)]
    queue.append(v)
    while queue:
        q = queue.popleft()
        while G[q]:
            nex = G[q].popleft()
            dif = used_color - node2colors[q]
            if dif:
                usd_c = dif.pop()
                dif.add(usd_c)
                E[(q, nex)][1] = usd_c
                node2colors[q].add(usd_c)
                node2colors[nex].add(usd_c)
            else:
                new_c = new_color
                new_color += 1
                E[(q, nex)][1] = new_c
                node2colors[q].add(new_c)
                node2colors[nex].add(new_c)
                used_color.add(new_c)
                K += 1
            queue.append(nex)
    return K

def main():
    N = int(input())
    G = [deque() for _ in range(N)]
    E = dict() # (a, b) -> [idx, color]
    for i in range(N-1):
        a, b = map(lambda x: int(x)-1, input().split())
        G[a].append(b)
        E[(a, b)] = [i, 0]
    K = bfs(0, N, G, E)
    # ans_list = [value for value in E.values()]
    print(K)
    for value in E.values():
        print(value[1])
main()

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline
    
def bfs(v, N, G, E):
    visited = [0]*N
    queue = deque()
    K = -1
    node2color = [-1 for _ in range(N)]
    queue.append(v)
    visited[v] = 1
    while queue:
        q = queue.popleft()
        color = 0
        for nex in G[q]:
            if visited[nex]:
                continue
            visited[nex] = 1
            color += 1
            if color == node2color[q]:
                color += 1
            
            node2color[nex] = color
            E[(min(q, nex), max(q, nex))] = color
            queue.append(nex)
        K = max(K, color)
    return K

def main():
    N = int(input())
    G = [deque() for _ in range(N)]
    E = dict() # (a, b) -> [idx, color]
    for i in range(N-1):
        a, b = map(lambda x: int(x)-1, input().split())
        G[a].append(b)
        G[b].append(a)
        E[(a, b)] = 0
    K = bfs(0, N, G, E)
    # ans_list = [value for value in E.values()]
    print(K)
    for value in E.values():
        print(value)
main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N = int(input())
G = [deque() for _ in range(N)]
E = dict() # (a, b) -> [idx, color]
for i in range(N-1):
    a, b = map(lambda x: int(x)-1, input().split())
    G[a].append(b)
    E[(a, b)] = [i, 0]
    

K = 0
new_color = [i for i in range(N-1, 0, -1)]
used_color = set()
queue = deque()
node2colors = [set() for _ in range(N)]
def bfs(v):
    global K
    # seen[v] = 1
    queue.append(v)
    while queue:
        q = queue.popleft()
        # if seen[q]:
            # continue
        while G[q]:
            nex = G[q].popleft()
            dif = used_color.difference(node2colors[q])
            if dif:
                usd_c = dif.pop()
                dif.add(usd_c)
                E[(q, nex)][1] = usd_c
                node2colors[q].add(usd_c)
                node2colors[nex].add(usd_c)
            else:
                new_c = new_color.pop()
                E[(q, nex)][1] = new_c
                node2colors[q].add(new_c)
                node2colors[nex].add(new_c)
                used_color.add(new_c)
                K += 1
            queue.append(nex)

bfs(0)
ans_list = [value for value in E.values()]
print(K)
for i in range(N-1):
    print(ans_list[i][1])

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline
    
def bfs(v, N, G, E):
    visited = [0]*N
    queue = deque()
    K = -1
    node2color = [-1 for _ in range(N)]
    queue.append(v)
    visited[v] = 1
    while queue:
        q = queue.popleft()
        color = 0
        for nex in G[q]:
            if visited[nex]:
                continue
            visited[nex] = 1
            color += 1
            if color == node2color[q]:
                color += 1
            
            node2color[nex] = color
            E[(min(q, nex), max(q, nex))] = color
            queue.append(nex)
        K = max(K, color)
    return K

def main():
    N = int(input())
    G = [deque() for _ in range(N)]
    E = dict() # (a, b) -> [idx, color]
    for i in range(N-1):
        a, b = map(lambda x: int(x)-1, input().split())
        G[a].append(b)
        G[b].append(a)
        E[(a, b)] = 0
    K = bfs(0, N, G, E)
    # ans_list = [value for value in E.values()]
    print(K)
    for value in E.values():
        print(value)
main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N = int(input())
G = [deque() for _ in range(N)]
E = dict() # (a, b) -> [idx, color]
for i in range(N-1):
    a, b = map(lambda x: int(x)-1, input().split())
    G[a].append(b)
    E[(a, b)] = [i, 0]
    

K = 0
new_color = [i for i in range(N-1, 0, -1)]
used_color = set()
queue = deque()
node2colors = [set() for _ in range(N)]
def bfs(v):
    global K
    # seen[v] = 1
    queue.append(v)
    while queue:
        q = queue.popleft()
        # if seen[q]:
            # continue
        for edge in range(len(G[q])):
            nex = G[q][edge]
            dif = used_color.difference(node2colors[q])
            if dif:
                usd_c = dif.pop()
                dif.add(usd_c)
                E[(q, nex)][1] = usd_c
                node2colors[q].add(usd_c)
                node2colors[nex].add(usd_c)
            else:
                new_c = new_color.pop()
                E[(q, nex)][1] = new_c
                node2colors[q].add(new_c)
                node2colors[nex].add(new_c)
                used_color.add(new_c)
                K += 1
            queue.append(nex)

bfs(0)
ans_list = [value for value in E.values()]
ans_list.sort()
print(K)
for i in range(N-1):
    print(ans_list[i][1])

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline
    
def bfs(v, N, G, E):
    visited = [0]*N
    queue = deque()
    K = -1
    node2color = [-1 for _ in range(N)]
    queue.append(v)
    visited[v] = 1
    while queue:
        q = queue.popleft()
        color = 0
        for nex in G[q]:
            if visited[nex]:
                continue
            visited[nex] = 1
            color += 1
            if color == node2color[q]:
                color += 1
            
            node2color[nex] = color
            E[(min(q, nex), max(q, nex))] = color
            queue.append(nex)
        K = max(K, color)
    return K

def main():
    N = int(input())
    G = [deque() for _ in range(N)]
    E = dict() # (a, b) -> [idx, color]
    for i in range(N-1):
        a, b = map(lambda x: int(x)-1, input().split())
        G[a].append(b)
        G[b].append(a)
        E[(a, b)] = 0
    K = bfs(0, N, G, E)
    # ans_list = [value for value in E.values()]
    print(K)
    for value in E.values():
        print(value)
main()