Problem:


Score : 400 points

Problem Statement

Given is a tree G with N vertices.
The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.

Consider painting the edges in G with some number of colors.
We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.

Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.

Constraints

 2 \le N \le 10^5

 1 \le a_i \lt b_i \le N

All values in input are integers.

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}

Output

Print N lines.

The first line should contain K, the number of colors used.

The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.

If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.

Sample Input 1

3
1 2
2 3

Sample Output 1

2
1
2

Sample Input 2

8
1 2
2 3
2 4
2 5
4 7
5 6
6 8

Sample Output 2

4
1
2
3
4
1
1
2

Sample Input 3

6
1 2
1 3
1 4
1 5
1 6

Sample Output 3

5
1
2
3
4
5



Sample input:
3
1 2
2 3


Sample output:
2
1
2

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
a_list=[]
b_list=[]
for i in range(N-1):
    ab = list(map(int,input().split()))
    a_list.append(ab[0])
    b_list.append(ab[1])

graph = [[] for i in range(N)]
for i in range(N-1):
    graph[a_list[i]-1].append((b_list[i],i))
    graph[b_list[i]-1].append((a_list[i],i))
    
ans = [0 for i in range(N-1)]
ten_col = [[] for i in range(N)]
def dfs(start,now):
    for (future,i) in graph[now-1]:
        if future == start:
            pass
        else:
            new_col = 1
            while True:
                if new_col not in ten_col[now-1]:
                    ten_col[now-1].append(new_col)
                    ten_col[future-1].append(new_col)
                    ans[i] = new_col
                    break
                else:
                    new_col += 1
            dfs(now,future)
            
dfs(-1,1)
print(max(ans))
for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
def color_edges(pivot_vertex, colors_available, edges_remaining):
    import random

    colors_available = colors_available.copy()

    for edge in edges_remaining:
        color = random.sample(colors_available, 1)[0]
        colors_available.remove(color)
        edges_colors[edge] = color
        if edge[0] == pivot_vertex:
            other_vertex = edge[1]
        else:
            other_vertex = edge[0]
        color_edges(
            other_vertex, colors - {color}, vertices_edges[other_vertex] - {edge}
        )


N = int(input())
edges = [tuple(map(int, input().split())) for _ in range(N - 1)]
vertices_edges = {}
for e in edges:
    a, b = e
    if a in vertices_edges:
        vertices_edges[a].add(e)
    else:
        vertices_edges[a] = {e}
    if b in vertices_edges:
        vertices_edges[b].add(e)
    else:
        vertices_edges[b] = {e}
color_variation = max(map(len, vertices_edges.values()))
colors = set(range(1, 1 + color_variation))
edges_colors = {e: 0 for e in edges}
color_edges(1, colors, vertices_edges[1])
print(color_variation)
for e in edges:
    print(edges_colors[e])

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
ab_list = [list(map(int,input().split())) for i in range(N-1)]

ab_list.sort()

graph = [[] for i in range(N)]
for i in range(N-1):
    graph[ab_list[i][0]-1].append((ab_list[i][1],i))
    graph[ab_list[i][1]-1].append((ab_list[i][0],i))
    
ans = [0 for i in range(N-1)]
ten_col = [[] for i in range(N)]
def dfs(start,now,color):
    ten_col[now-1].append(color)
    for (future,i) in graph[now-1]:
        if future == start:
            pass
        else:
            new_col = 1
            while True:
                if new_col not in ten_col[now-1]:
                    ten_col[now-1].append(new_col)
                    ten_col[future-1].append(new_col)
                    ans[i] = new_col
                    break
                else:
                    new_col += 1
            dfs(now,future,new_col)
            
dfs(-1,1,0)
print(max(ans))
for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
ab_list = [list(map(int,input().split())) for i in range(N-1)]

ab_list.sort()

graph = [[] for i in range(N)]
for i in range(N-1):
    graph[ab_list[i][0]-1].append((ab_list[i][1],i))
    graph[ab_list[i][1]-1].append((ab_list[i][0],i))
    
ans = [0 for i in range(N-1)]
ten_col = [[] for i in range(N)]
def dfs(start,now,color):
    ten_col[now-1].append(color)
    for (future,i) in graph[now-1]:
        if future == start:
            pass
        else:
            new_col = 1
            while True:
                if new_col not in ten_col[now-1]:
                    ten_col[now-1].append(new_col)
                    ans[i] = new_col
                    break
                else:
                    new_col += 1
            dfs(now,future,new_col)
            
dfs(-1,1,0)
print(max(ans))
for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
N = int(input())
ab_list = [list(map(int,input().split())) for i in range(N-1)]

ab_list.sort()

graph = [[] for i in range(N)]
for i in range(N-1):
    graph[ab_list[i][0]-1].append((ab_list[i][1],i))
    graph[ab_list[i][1]-1].append((ab_list[i][0],i))
    
ans = [0 for i in range(N-1)]
ten_col = [[] for i in range(N)]
def dfs(start,now,color):
    ten_col[now-1]=[color]
    for (future,i) in graph[now-1]:
        if future == start:
            pass
        else:
            new_col = 1
            while True:
                if new_col not in ten_col[now-1]:
                    ten_col[now-1].append(new_col)
                    ans[i] = new_col
                    break
                else:
                    new_col += 1
            dfs(now,future,new_col)
            
dfs(-1,1,0)
print(max(ans))
for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
import sys
N = int(input().strip())

n_list = [[] for i in range(N)]
d = [0] * N

for i in range(N-1):
    a, b = map(int, input().strip().split())
    a -= 1
    b -= 1
    n_list[a].append([b,i])
    n_list[b].append([a,i])
    d[a] += 1
    d[b] += 1


#print(n_list)
#print(d)

d_dict = {}
for i in range(N):
    d_dict[i] = d[i]
tmp = sorted(d_dict.items(), key = lambda x:x[1], reverse = True)

colors = [0] * (N)

root = tmp[0][0]

def paint(node, parent_edge_id):
    #print(node, parent_edge_id)
    tmp_color = 1
    for edge in n_list[node]:
        #print("ege:", edge)
        if edge[1] != parent_edge_id:
            if parent_edge_id != -1 and tmp_color == colors[parent_edge_id]:
                tmp_color += 1
            colors[edge[1]] = tmp_color
            paint(edge[0], edge[1])
            tmp_color += 1

    
#sys.setrecursionlimit(10**7)
try:
    paint(tmp[0][0], -1)
except RecursionError as e:
    print(e)
    print(0)
    exit(0)
print(tmp[0][1]) # color num 
for i in range(N-1):
    print(colors[i])

Submission status: Runtime Error

Solution code:
N = int(input())
ab_list = [list(map(int,input().split())) for i in range(N-1)]

ab_list.sort()

graph = [[] for i in range(N)]
for i in ab_list:
    graph[i[0]-1].append(i[1])
    graph[i[1]-1].append(i[0])
    
ans = []
ten_col = [[] for i in range(N)]
def dfs(start,now,color):
    ten_col[now-1]=[color]
    for i in graph[now-1]:
        if i == start:
            pass
        else:
            new_col = 1
            while True:
                if new_col not in ten_col[now-1]:
                    ten_col[now-1].append(new_col)
                    ans.append(new_col)
                    break
                else:
                    new_col += 1
            dfs(now,i,new_col)
            
dfs(-1,1,0)
print(max(ans))
for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
import sys
N = int(input().strip())

n_list = [[] for i in range(N)]
d = [0] * N

for i in range(N-1):
    a, b = map(int, input().strip().split())
    a -= 1
    b -= 1
    n_list[a].append([b,i])
    n_list[b].append([a,i])
    d[a] += 1
    d[b] += 1


#print(n_list)
#print(d)

d_dict = {}
for i in range(N):
    d_dict[i] = d[i]
tmp = sorted(d_dict.items(), key = lambda x:x[1], reverse = True)

colors = [0] * (N)

root = tmp[0][0]

def paint(node, parent_edge_id):
    #print(node, parent_edge_id)
    tmp_color = 1
    for edge in n_list[node]:
        #print("ege:", edge)
        if edge[1] != parent_edge_id:
            if parent_edge_id != -1 and tmp_color == colors[parent_edge_id]:
                tmp_color += 1
            colors[edge[1]] = tmp_color
            paint(edge[0], edge[1])
            tmp_color += 1

    
#sys.setrecursionlimit(10**7)
try:
    paint(tmp[0][0], -1)
except RecursionError:
    print(0)
    exit(0)
print(tmp[0][1]) # color num 
for i in range(N-1):
    print(colors[i])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3


def f(a, b):
    return (a, b) if a < b else (b, a)


def dfs(s, p):
    for k, t in enumerate(g[s]):
        idx = rev[f(s, t)]
        if t == p or ans[idx] > 0:
            continue
        ans[idx] = k + 1
        dfs(t, s)

n = int(input())
g = [[] for _ in range(n)]
rev = {}
for i in range(n - 1):
    a, b = map(lambda x: int(x) - 1, input().split())
    g[a].append(b)
    g[b].append(a)
    rev[(a, b)] = i
ans = [0] * (n - 1)
dfs(0, -1)
print(max(ans), *ans, sep='\n')

Submission status: Runtime Error

Solution code:
from operator import itemgetter

N = int(input())


A = []
for i in range(N - 1):
    A.append([int(i) for i in input().split()])

B = []
for row in A:
    B.append(sorted(row))
B.sort(key = itemgetter(0))

link_list = [[] for i in range(N)]
for row in B:
    link_list[row[0] - 1].append(row[1] - 1)
    link_list[row[1] - 1].append(row[0] - 1)
edge_color = dict()
marker = [0] * N


def DFS_color(node, pre_color):
    target_list = link_list[node]
    color = 1
    for ko_node in target_list:
        if marker[ko_node] == 0:
            if pre_color == color:
                color += 1
            #edge_color[node, ko_node] = color
            #edge_color[ko_node, node] = color 
            marker[ko_node] = 1
            DFS_color(ko_node, color)
            color += 1
        else:
            pass

marker[0] = 1
DFS_color(0, 0)
"""
print(max(edge_color.values()))
for node1, node2 in A:
    print(edge_color[node1 - 1, node2 - 1])
"""

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**9)

n=int(input())
# n=100000
tree=[set() for i in range(n)]
edges=[tuple(map(int,input().split())) for i in range(n-1)]
edge_color={}
for a,b in edges:
    a-=1
    b-=1
    tree[a].add(b)
    tree[b].add(a)
k = 0
for i in range(n):
    k = max(k,len(tree[i]))

for e in edges:
    edge_color[e] = 0

def colorize(u,v,c):
    edge_color[(min(u,v),max(u,v))] = c
    cc = c+1
    if cc > k:
        cc = 1
    for x in tree[u]:
        if edge_color[(min(x,u),max(x,u))] == 0:
            colorize(x,u,cc)
            cc += 1
            if cc > k:
                cc = 1

colorize(0,1,1)
print(k)
for e in edges:
    print(edge_color[e])

Submission status: Runtime Error

Solution code:
from operator import itemgetter

N = int(input())


A = []
for i in range(N - 1):
    A.append([int(i) for i in input().split()])

B = []
for row in A:
    B.append(sorted(row))
B.sort(key = itemgetter(0))

link_list = [[] for i in range(N)]
for row in B:
    link_list[row[0] - 1].append(row[1] - 1)
    link_list[row[1] - 1].append(row[0] - 1)
edge_color = dict()
marker = [0] * N


def DFS_color(node, pre_color):
    target_list = link_list[node]
    color = 1
    for ko_node in target_list:
        if marker[ko_node] == 0:
            if pre_color == color:
                color += 1
            edge_color[node, ko_node] = color
            edge_color[ko_node, node] = color 
            marker[ko_node] = 1
            DFS_color(ko_node, color)
            color += 1
        else:
            pass

marker[0] = 1
DFS_color(0, 0)
"""
print(max(edge_color.values()))
for node1, node2 in A:
    print(edge_color[node1 - 1, node2 - 1])
"""

Submission status: Runtime Error

Solution code:
from operator import itemgetter

N = int(input())


A = []
for i in range(N - 1):
    A.append([int(i) for i in input().split()])

B = []
for row in A:
    B.append(sorted(row))
B.sort(key = itemgetter(0))

link_list = [[] for i in range(N)]
for row in B:
    link_list[row[0] - 1].append(row[1] - 1)
    link_list[row[1] - 1].append(row[0] - 1)
edge_color = dict()
marker = [0] * N

def DFS_color(node, pre_color):
    target_list = link_list[node]
    color = 1
    for ko_node in target_list:
        if marker[ko_node] == 0:
            if pre_color == color:
                color += 1
            edge_color[node, ko_node] = color
            edge_color[ko_node, node] = color 
            marker[ko_node] = 1
            DFS_color(ko_node, color)
            color += 1
        else:
            pass

marker[0] = 1
DFS_color(0, 0)

print(1)
#print(max(edge_color.values()))
for node1, node2 in A:
    if (node1-1, node2-1) in edge_color:
        print(edge_color[node1 - 1, node2 - 1])
    else:
        print("a")

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.read


n, *ab = map(int, read().split())
ans = [0]*(n-1)
edges = [[]for _ in range(n)]
used = [0]*n

for i, a, b in zip(range(n-1), *[iter(ab)] * 2):
    edges[a].append([b, i])

for node in range(n):
    unable = used[node]
    color = 1

    for to, id in edges[node]:
        if color == unable:
            color += 1
        ans[id] = str(color)
        used[to] = color
        color += 1

print(max(used))
print('\n'.join(ans))

Submission status: Runtime Error

Solution code:
from collections import Counter, defaultdict
G = defaultdict(list)
C = Counter()
N = int(input())
ans = {}
o = []
for i in range(N-1):
    a, b = map(int, input().split())
    C[a] += 1
    C[b] += 1
    G[a].append(b)
    G[b].append(a)
    ans[(a, b)] = 0
    o.append((a,b))

a = C.most_common(1)[0][0]
b = C.most_common(1)[0][1]

visited = [False] * (N+1)
def dfs(v, p):
    visited[v] = True
    counter = 1
    for c in G[v]:
        if not visited[c]:
            if p != 0 and counter == ans[(v, p)]:
                counter += 1
            ans[(v, c)] = ans[(c, v)] = counter
            counter += 1
            dfs(c, v)

dfs(a, 0)

print(b)
for a,b in o:
    print(ans[(a,b)])

Submission status: Runtime Error

Solution code:
N = int(input().strip())

n_list = [[] for i in range(N)]
d = [0] * N

for i in range(N-1):
    a, b = map(int, input().strip().split())
    a -= 1
    b -= 1
    n_list[a].append([b,i])
    n_list[b].append([a,i])
    d[a] += 1
    d[b] += 1


#print(n_list)
#print(d)

d_dict = {}
for i in range(N):
    d_dict[i] = d[i]
tmp = sorted(d_dict.items(), key = lambda x:x[1], reverse = True)

colors = [0] * (N)

root = tmp[0][0]

def paint(node, parent_edge_id):
    tmp_color = 1
    for edge in n_list[node]:
        try:
            if edge[1] == parent_edge_id:
                continue
            if parent_edge_id != -1 and tmp_color == colors[parent_edge_id]:
                tmp_color += 1
            colors[edge[1]] = tmp_color
            tmp_color += 1
        except Exception:
            print(0)
        paint(edge[0], edge[1])

    
paint(tmp[0][0], -1)
print(tmp[0][1]) # color num    a
for i in range(N-1):
    print(colors[i])

Submission status: Runtime Error

Solution code:
N = int(input())

E = []
T = [[] for i in range(N)]
for i,n in enumerate(range(N-1)):
    a,b = map(int,input().split())
    E.append((a-1,b-1))
    T[a-1].append(b-1)

# print(E)
# print(T)



R = [None for i in range(N)]
def dfs(using, cur):
    R[cur] = using
    i=0
    for t in T[cur]:
        if i==using:
            i+=1
        dfs(i,t)
        i+=1


dfs(-1, 0)

print(max(R)+1)
# print(K)
for e in E:
    print(R[e[1]]+1)

# print(K)

Submission status: Runtime Error

Solution code:
from operator import itemgetter

N = int(input())


A = []
for i in range(N - 1):
    A.append([int(i) for i in input().split()])

B = []
for row in A:
    B.append(sorted(row))
B.sort(key = itemgetter(0))

link_list = [[] for i in range(N)]
for row in B:
    link_list[row[0] - 1].append(row[1] - 1)
    link_list[row[1] - 1].append(row[0] - 1)
edge_color = dict()
marker = [0] * N

def DFS_color(node, pre_color):
    target_list = link_list[node]
    color = 1
    for ko_node in target_list:
        if marker[ko_node] == 0:
            if pre_color == color:
                color += 1
            edge_color[node, ko_node] = color
            edge_color[ko_node, node] = color 
            marker[ko_node] = 1
            DFS_color(ko_node, color)
            color += 1
        else:
            pass

marker[0] = 1
DFS_color(0, 0)

print(max(edge_color.values()))
for node1, node2 in A:
    if (node1-1, node2-1) in edge_color:
        print(edge_color[node1 - 1, node2 - 1])
    else:
        print("a")

Submission status: Runtime Error

Solution code:
N = int(input().strip())

n_list = [[] for i in range(N)]
d = [0] * N

for i in range(N-1):
    a, b = map(int, input().strip().split())
    a -= 1
    b -= 1
    n_list[a].append([b,i])
    n_list[b].append([a,i])
    d[a] += 1
    d[b] += 1


#print(n_list)
#print(d)

d_dict = {}
for i in range(N):
    d_dict[i] = d[i]
tmp = sorted(d_dict.items(), key = lambda x:x[1], reverse = True)

colors = [0] * (N)

root = tmp[0][0]

def paint(node, parent_edge_id):
    tmp_color = 1
    for edge in n_list[node]:
        try:
            if edge[1] == parent_edge_id:
                continue
            if parent_edge_id != -1 and tmp_color == colors[parent_edge_id]:
                tmp_color += 1
            colors[edge[1]] = tmp_color
        except Exception:
            print(0)
        paint(edge[0], edge[1])
        tmp_color += 1

    
paint(tmp[0][0], -1)
print(tmp[0][1]) # color num    a
for i in range(N-1):
    print(colors[i])

Submission status: Runtime Error

Solution code:
N = int(input().strip())

n_list = [[] for i in range(N)]
d = [0] * N

for i in range(N-1):
    a, b = map(int, input().strip().split())
    a -= 1
    b -= 1
    n_list[a].append([b,i])
    n_list[b].append([a,i])
    d[a] += 1
    d[b] += 1


#print(n_list)
#print(d)

d_dict = {}
for i in range(N):
    d_dict[i] = d[i]
tmp = sorted(d_dict.items(), key = lambda x:x[1], reverse = True)

colors = [0] * (N)

root = tmp[0][0]

def paint(node, parent_edge_id):
    tmp_color = 1
    for edge in n_list[node]:
        if edge[1] == parent_edge_id:
            continue
        try:
            if parent_edge_id != -1 and tmp_color == colors[parent_edge_id]:
                tmp_color += 1
            colors[edge[1]] = tmp_color
        except Exception:
            print(0)
        paint(edge[0], edge[1])
        tmp_color += 1

    
paint(tmp[0][0], -1)
print(tmp[0][1]) # color num    a
for i in range(N-1):
    print(colors[i])

Submission status: Runtime Error

Solution code:
N = int(input().strip())

n_list = [[] for i in range(N)]
d = [0] * N

for i in range(N-1):
    a, b = map(int, input().strip().split())
    a -= 1
    b -= 1
    n_list[a].append([b,i])
    n_list[b].append([a,i])
    d[a] += 1
    d[b] += 1


#print(n_list)
#print(d)

d_dict = {}
for i in range(N):
    d_dict[i] = d[i]
tmp = sorted(d_dict.items(), key = lambda x:x[1], reverse = True)

colors = [0] * (N)

root = tmp[0][0]

def paint(node, parent_edge_id):
    tmp_color = 1
    for edge in n_list[node]:
        if edge[1] != parent_edge_id:
            if parent_edge_id != -1 and tmp_color == colors[parent_edge_id]:
                tmp_color += 1
            colors[edge[1]] = tmp_color
            try:
                paint(edge[0], edge[1])
            except RecursionError:
                print(0)
            tmp_color += 1

    
paint(tmp[0][0], -1)
print(tmp[0][1]) # color num    a
for i in range(N-1):
    print(colors[i])

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

def main():

    """
    # color: 親と自らを繋ぐ辺の色
    # now: 現在地
    """
    def dfs(color,now):
        # 現在地が葉のとき
        if not now in a:
            return

        # 現在地から伸びる辺のインデックス
        index = [i for i in range(N-1) if now == a[i]]

        # 子ノードに伸びる辺を色塗り
        number = 1
        for i in range(len(index)):
            if number == color:
                number += 1
            K[index[i]] = number
            # 子ノードで色塗り開始
            dfs(number, b[index[i]])
            number += 1

    N = int(input())
    a = [0] * (N-1)
    b = [0] * (N-1)
    for i in range(N-1):
        a[i],b[i] = map(int,input().split())
    # 辺の色
    K = [0] * (N-1)
    dfs(0,1)
    print(len(set(K)))
    for i in K:
        print(i)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from operator import itemgetter

N = int(input())


A = []
for i in range(N - 1):
    A.append([int(i) for i in input().split()])

B = []
for row in A:
    B.append(sorted(row))
B.sort(key = itemgetter(0))

link_list = [[] for i in range(N)]
for row in B:
    link_list[row[0] - 1].append(row[1] - 1)
    link_list[row[1] - 1].append(row[0] - 1)
edge_color = dict()
marker = [0] * N

def DFS_color(node, pre_color):
    target_list = link_list[node]
    color = 1
    for ko_node in target_list:
        if marker[ko_node] == 0:
            if pre_color == color:
                color += 1
            edge_color[node, ko_node] = color
            edge_color[ko_node, node] = color 
            marker[ko_node] = 1
            DFS_color(ko_node, color)
            color += 1
        else:
            pass

marker[0] = 1
DFS_color(0, 0)

print(max(edge_color.values()))
for node1, node2 in A:
    print(edge_color[node1 - 1, node2 - 1])

Submission status: Runtime Error

Solution code:
from operator import itemgetter

N = int(input())


A = []
for i in range(N - 1):
    A.append([int(i) for i in input().split()])

print("==")

B = []
for row in A:
    B.append(sorted(row))
B.sort(key = itemgetter(0))

link_list = [[] for i in range(N)]
for row in B:
    link_list[row[0] - 1].append(row[1] - 1)
    link_list[row[1] - 1].append(row[0] - 1)
edge_color = dict()
marker = [0] * N

def DFS_color(node, pre_color):
    target_list = link_list[node]
    print(target_list, node)
    color = 1
    for ko_node in target_list:
        if marker[ko_node] == 0:
            if pre_color == color:
                color += 1
            edge_color[node, ko_node] = color
            edge_color[ko_node, node] = color 
            print(node, ko_node, color)
            marker[ko_node] = 1
            DFS_color(ko_node, color)
            color += 1
        else:
            pass

marker[0] = 1
DFS_color(0, 0)

print(max(edge_color.values()))
for node1, node2 in A:
    print(edge_color[node1 - 1, node2 - 1])

Submission status: Runtime Error

Solution code:
iro = []
iro_i = []
res = []
r = 0
cm = []
for i in range(N+1):
  cm.append([])
N = int(input())
for i in range(N-1):
  a,b = input().split(" ")
  a = int(a)
  b = int(b)
  for cr in set(iro_i) - set(cm[a] + cm[b]):
    res.append(cr+1)
    cm[a].append(cr)
    cm[b].append(cr)
    break
  else:
    iro_i.append(r)
    cm[a].append(r)
    cm[b].append(r)
    res.append(r+1)
    r+=1
print(r+1)
for i in res:
  print(i)

Submission status: Runtime Error

Solution code:
N = int(input().strip())

n_list = [[] for i in range(N)]
d = [0] * N

for i in range(N-1):
    a, b = map(int, input().strip().split())
    a -= 1
    b -= 1
    n_list[a].append([b,i])
    n_list[b].append([a,i])
    d[a] += 1
    d[b] += 1


#print(n_list)
#print(d)

d_dict = {}
for i in range(N):
    d_dict[i] = d[i]
tmp = sorted(d_dict.items(), key = lambda x:x[1], reverse = True)

colors = [0] * (N)

root = tmp[0][0]

def paint(node, parent_edge_id):
    tmp_color = 1
    for edge in n_list[node]:
        if edge[1] != parent_edge_id:
            if parent_edge_id != -1 and tmp_color == colors[parent_edge_id]:
                tmp_color += 1
            colors[edge[1]] = tmp_color
            paint(edge[0], edge[1])
            tmp_color += 1

    
try:
    paint(tmp[0][0], -1)
except TypeError:
    print(0)
print(tmp[0][1]) # color num    a
for i in range(N-1):
    print(colors[i])

Submission status: Runtime Error

Solution code:
N = int(input())
G = [[] for _ in range(N+1)]

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

color = max(len(g) for g in G)
print(color)

ans = [-1] * (N-1)

def dfs(v=1, p=0, p_col=-1):
    col = 1

    for side, idx in G[v]:
        if side != p:
            if col == p_col:
                col += 1
            ans[idx] = col
            dfs(side, v, col)
            col += 1

dfs()
print("\n".join(map(str, ans)))

Submission status: Runtime Error

Solution code:
N = int(input().strip())

n_list = [[] for i in range(N)]
d = [0] * N

for i in range(N-1):
    a, b = map(int, input().strip().split())
    a -= 1
    b -= 1
    n_list[a].append([b,i])
    n_list[b].append([a,i])
    d[a] += 1
    d[b] += 1


#print(n_list)
#print(d)

d_dict = {}
for i in range(N):
    d_dict[i] = d[i]
tmp = sorted(d_dict.items(), key = lambda x:x[1], reverse = True)

colors = [0] * (N)

root = tmp[0][0]

def paint(node, parent_edge_id):
    tmp_color = 1
    for edge in n_list[node]:
        if edge[1] != parent_edge_id:
            if parent_edge_id != -1 and tmp_color == colors[parent_edge_id]:
                tmp_color += 1
            colors[edge[1]] = tmp_color
            paint(edge[0], edge[1])
            tmp_color += 1

    
try:
    paint(tmp[0][0], -1)
except AttributeError:
    print(0)
print(tmp[0][1]) # color num    a
for i in range(N-1):
    print(colors[i])

Submission status: Runtime Error

Solution code:
N = int(input().strip())

n_list = [[] for i in range(N)]
d = [0] * N

for i in range(N-1):
    a, b = map(int, input().strip().split())
    a -= 1
    b -= 1
    n_list[a].append([b,i])
    n_list[b].append([a,i])
    d[a] += 1
    d[b] += 1


#print(n_list)
#print(d)

d_dict = {}
for i in range(N):
    d_dict[i] = d[i]
tmp = sorted(d_dict.items(), key = lambda x:x[1], reverse = True)

colors = [0] * (N)

root = tmp[0][0]

def paint(node, parent_edge_id):
    #print(node, parent_edge_id)
    tmp_color = 1
    for edge in n_list[node]:
        #print("ege:", edge)
        if edge[1] != parent_edge_id:
            if parent_edge_id != -1 and tmp_color == colors[parent_edge_id]:
                tmp_color += 1
            colors[edge[1]] = tmp_color
            paint(edge[0], edge[1])
            tmp_color += 1

    
try:
    paint(tmp[0][0], -1)
except ValueError:
    print(0)
print(tmp[0][1]) # color num    a
for i in range(N-1):
    print(colors[i])

Submission status: Runtime Error

Solution code:
N = int(input().strip())

n_list = [[] for i in range(N)]
d = [0] * N

for i in range(N-1):
    a, b = map(int, input().strip().split())
    a -= 1
    b -= 1
    n_list[a].append([b,i])
    n_list[b].append([a,i])
    d[a] += 1
    d[b] += 1


#print(n_list)
#print(d)

d_dict = {}
for i in range(N):
    d_dict[i] = d[i]
tmp = sorted(d_dict.items(), key = lambda x:x[1], reverse = True)

colors = [0] * (N)

root = tmp[0][0]

def paint(node, parent_edge_id):
    #print(node, parent_edge_id)
    tmp_color = 1
    for edge in n_list[node]:
        #print("ege:", edge)
        if edge[1] != parent_edge_id:
            if parent_edge_id != -1 and tmp_color == colors[parent_edge_id]:
                tmp_color += 1
            colors[edge[1]] = tmp_color
            paint(edge[0], edge[1])
            tmp_color += 1

    
try:
    paint(tmp[0][0], -1)
except TypeError:
    print(0)
print(tmp[0][1]) # color num    a
for i in range(N-1):
    print(colors[i])

Submission status: Runtime Error

Solution code:
N = int(input().strip())

n_list = [[] for i in range(N)]
d = [0] * N

for i in range(N-1):
    a, b = map(int, input().strip().split())
    a -= 1
    b -= 1
    n_list[a].append([b,i])
    n_list[b].append([a,i])
    d[a] += 1
    d[b] += 1


#print(n_list)
#print(d)

d_dict = {}
for i in range(N):
    d_dict[i] = d[i]
tmp = sorted(d_dict.items(), key = lambda x:x[1], reverse = True)

colors = [0] * (N)

root = tmp[0][0]

def paint(node, parent_edge_id):
    #print(node, parent_edge_id)
    tmp_color = 1
    for edge in n_list[node]:
        #print("ege:", edge)
        if edge[1] != parent_edge_id:
            if parent_edge_id != -1 and tmp_color == colors[parent_edge_id]:
                tmp_color += 1
            colors[edge[1]] = tmp_color
            paint(edge[0], edge[1])
            tmp_color += 1

    
try:
    paint(tmp[0][0], -1)
except SyntaxError:
    print(0)
print(tmp[0][1]) # color num    a
for i in range(N-1):
    print(colors[i])

Submission status: Runtime Error

Solution code:
N = int(input().strip())

n_list = [[] for i in range(N)]
d = [0] * N

for i in range(N-1):
    a, b = map(int, input().strip().split())
    a -= 1
    b -= 1
    n_list[a].append([b,i])
    n_list[b].append([a,i])
    d[a] += 1
    d[b] += 1


#print(n_list)
#print(d)

d_dict = {}
for i in range(N):
    d_dict[i] = d[i]
tmp = sorted(d_dict.items(), key = lambda x:x[1], reverse = True)

colors = [0] * (N)

root = tmp[0][0]

def paint(node, parent_edge_id):
    #print(node, parent_edge_id)
    tmp_color = 1
    for edge in n_list[node]:
        #print("ege:", edge)
        if edge[1] != parent_edge_id:
            if parent_edge_id != -1 and tmp_color == colors[parent_edge_id]:
                tmp_color += 1
            colors[edge[1]] = tmp_color
            paint(edge[0], edge[1])
            tmp_color += 1

    
try:
    paint(tmp[0][0], -1)
except KeyError:
    print(0)
print(tmp[0][1]) # color num    a
for i in range(N-1):
    print(colors[i])

Submission status: Runtime Error

Solution code:
N = int(input().strip())

n_list = [[] for i in range(N)]
d = [0] * N

for i in range(N-1):
    a, b = map(int, input().strip().split())
    a -= 1
    b -= 1
    n_list[a].append([b,i])
    n_list[b].append([a,i])
    d[a] += 1
    d[b] += 1


#print(n_list)
#print(d)

d_dict = {}
for i in range(N):
    d_dict[i] = d[i]
tmp = sorted(d_dict.items(), key = lambda x:x[1], reverse = True)

colors = [0] * (N)

root = tmp[0][0]

def paint(node, parent_edge_id):
    #print(node, parent_edge_id)
    tmp_color = 1
    for edge in n_list[node]:
        #print("ege:", edge)
        if edge[1] != parent_edge_id:
            if parent_edge_id != -1 and tmp_color == colors[parent_edge_id]:
                tmp_color += 1
            colors[edge[1]] = tmp_color
            paint(edge[0], edge[1])
            tmp_color += 1

    
try:
    paint(tmp[0][0], -1)
except RecursionError:
    print(0)
print(tmp[0][1]) # color num 
for i in range(N-1):
    print(colors[i])