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

Solution (Python):
import sys
sys.setrecursionlimit(200005)

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n = ni()
g = [[] for _ in range(n)]
for i in range(n-1):
    a, b = na()
    g[a-1].append(b-1<<32|i)
    g[b-1].append(a-1<<32|i)
    
    


def dfs(cur, pre, prec, g, colors):
    gen = 1
    for e in g[cur]:
        if e>>32 != pre:
            if gen == prec:
                gen += 1
            colors[e&(1<<32)-1] = gen
            dfs(e>>32, cur, gen, g, colors)
            gen += 1

colors = [0] * (n-1)
dfs(0, -1, 0, g, colors)

print(max(colors))
for v in colors:
    print(v)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
N = int(input())
d = [0]*N
G = [[] for _ in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    d[a-1]+=1
    d[b-1]+=1
    G[a-1].append((i, b-1))
    G[b-1].append((i, a-1))
K = max(d)
ans = [0]*(N-1)
def dfs(v, p, c):
    if G[v] == [p]:
        return 
    nc = 1
    for i, nv in G[v]:
        if nv == p:
            continue
        if nc == c:
            nc+=1
        ans[i] = nc
        dfs(nv, v, nc)
        nc+=1
    return
dfs(0, -1, -1)
print(K)
print(*ans, sep = '\n')

Solution (Python):
N = int(input())
AB = [list(map(int,input().split())) for _ in [0]*(N-1)]

E = [[] for _ in [0]*N]
for i,ab in enumerate(AB):
    a,b = ab
    a -= 1
    b -= 1
    E[a].append((b,i))
    E[b].append((a,i))

done = [False]*N
done[0] = True
ans = [0]*(N-1)
q = [(0,0)]
while q:
    i,c0 = q.pop()
    c = 1
    for j,ei in E[i]:
        if done[j]:continue
        done[j] = True
        if c==c0: c+=1
        q.append((j,c))
        ans[ei] = c
        c += 1

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

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

d = defaultdict(lambda : -1)
def solve():
    global d
    def dfs(x,pre,k,m):
        global d
        p = 1
        if pre != -1:
            if x < pre:
                d[(x,pre)] = k
            else:
                d[(pre,x)] = k
        for y in v[x]:
            if p == k:
                p += 1
            if m[y]:continue
            m[y] = 1
            dfs(y,x,p,m)
            p += 1
    n = I()
    v = [[] for i in range(n)]
    e = []
    f = [0]*n
    for i in range(n-1):
        a,b = LI()
        a -= 1
        b -= 1
        v[a].append(b)
        v[b].append(a)
        e.append((a,b))
        f[a] += 1
        f[b] += 1
    if n == 2:
        print(1)
        print(1)
        return
    m = [0]*n
    m[0] = 1
    dfs(0,-1,0,m)
    print(max(f))
    for x,y in e:
        print(d[(x,y)])
    return


if __name__ == "__main__":
    solve()

Solution (Python):
from collections import deque


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

tree = [[] for i in range(n)]
for i in range(n-1):
    a, b = info[i]
    a -= 1
    b -= 1
    tree[a].append(b)
    tree[b].append(a)
ans = 0
for i in range(n):
    ans = max(ans, len(tree[i]))

memo = {}
for i in range(n-1):
    tmp = tuple(sorted(info[i]))
    memo[tmp] = i

edge = [-1]*(n-1)

q = deque([0])
while q:
    pos = q.pop()
    li = []
    position = []
    used = set([])
    for next_pos in tree[pos]:
        tmp = tuple(sorted([next_pos+1, pos+1]))
        if edge[memo[tmp]] == -1:
            li.append(tmp)
            position.append(next_pos)
        else:
            used.add(edge[memo[tuple(sorted([next_pos+1, pos+1]))]])
    cnt = 1
    for i in li:
        while True:
            if cnt not in used:
                edge[memo[i]] = cnt
                cnt += 1
                break
            else:
                cnt += 1
    for i in position:
        q.append(i)

print(ans)
for i in edge:
    print(i)

Solution (Python):
n,*t=map(int,open(0).read().split())
e=[[]for _ in range(n)]
d=[]
for a,b in zip(*[iter(t)]*2):
    e[a-1]+=b-1,
    e[b-1]+=a-1,
    d+=(a-1,b-1),
s=[(0,0)]
f=[1]+[0]*n
dic=dict()
while s:
    v,color=s.pop()
    i=0
    for w in e[v]:
        if f[w]:continue
        f[w]=1
        i+=1
        if i==color:i+=1
        s+=(w,i),
        dic[v,w]=i
ans=[]
for a,b in d:
    if (a,b)in dic:
        ans+=dic[a,b],
    else:
        ans+=dic[b,a],
print(max(ans))
for t in ans:print(t)

Solution (Python):
import sys
sys.setrecursionlimit(10000000)

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

def dfs(node, parent):
    used = {col[(node, parent)]}
    curcol = 1
    for n in edges[node]:
        if n == parent: continue

        while curcol in used: curcol += 1

        col[(node, n)] = curcol
        col[(n, node)] = curcol

        dfs(n, node)

        used.add(curcol)
        
col = {}
start = max(range(1, N+1), key = lambda x : len(edges[x]))

for i, n in enumerate(edges[start]):
    col[(start, n)] = i + 1
    col[(n, start)] = i + 1
    dfs(n, start)



done = set()
print(len(edges[start]))
for e in alledges:
    print(col[e])

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
m = map(int,read().split())
AB = list(zip(m,m))

graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

root = 1
parent = [0] * (N+1)
order = []
stack = [root]
while stack:
    x = stack.pop()
    order.append(x)
    for y in graph[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        stack.append(y)

color = [-1] * (N+1)
for x in order:
    ng = color[x]
    c = 1
    for y in graph[x]:
        if y == parent[x]:
            continue
        if c == ng:
            c += 1
        color[y] = c
        c += 1

answer = []
append = answer.append
for a,b in AB:
    if parent[a] == b:
        append(color[a])
    else:
        append(color[b])

K = max(answer)
print(K)
print('\n'.join(map(str,answer)))

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)


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

ans = [None]*(n-1)
def rec(cur,color):
    cnt = 1
    for (to, j) in G[cur]:
        if cnt == color:
            cnt += 1
        ans[j] = cnt
        rec(to, cnt)
        cnt += 1
rec(0, 0)
print(max(ans))
print(*ans, sep="\n")

Solution (Python):
import sys

n = int(input())
links = [set() for _ in range(n)]
for i, line in enumerate(sys.stdin):
    a, b = map(int, line.split())
    a -= 1
    b -= 1
    links[a].add((i, b))
    links[b].add((i, a))

ans = [-1] * (n - 1)
k = max(map(len, links))
q = [(0, -1)]
fixed = set()

while q:
    v, c = q.pop()
    color = 1
    if c == color:
        color += 1
    for i, u in links[v]:
        if ans[i] != -1:
            continue
        ans[i] = color
        q.append((u, color))
        color += 1
        if c == color:
            color += 1

print(k)
print('\n'.join(map(str, ans)))

Solution (Python):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


N = int(sys.stdin.buffer.readline())
AB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(N - 1)]
graph = [[] for _ in range(N)]
idx = {}
for i, (a, b) in enumerate(AB):
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)
    idx[(a, b)] = i
    idx[(b, a)] = i

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

stack = [(0, -1)]
seen = [False] * N
while stack:
    v, color = stack.pop()
    seen[v] = True
    c = 0
    for u in graph[v]:
        if seen[u]:
            continue
        c += 1
        if c == color:
            c += 1
        ans[idx[(v, u)]] = c
        stack.append((u, c))
print(max(ans))
print(*ans, sep='\n')

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
n = int(input())
E = [[] for _ in range(n)]
C = [0 for i in range(n-1)]
for i in range(n-1):
    a,b = map(int, input().split())
    E[a-1].append([b-1, i])
    E[b-1].append([a-1, i])
c = 1
def dfs(cur, pre, pre_c):
    c = 1
    for e, i in E[cur]:
        if e != pre:
            if c == pre_c:
                c += 1
            C[i] = c
            dfs(e, cur, c)
            c += 1

dfs(0, -1, 0)
print(max(C))
for item in C:
    print(item)

Solution (Python):
import sys
from collections import deque
input = sys.stdin.buffer.readline

N = int(input())
adj = [[] for _ in range(N+1)]
edge = {}
for i in range(N-1):
    a, b = map(int, input().split())
    adj[a].append(b)
    adj[b].append(a)
    edge[(a, b)] = i
    edge[(b, a)] = i

que = deque()
que.append(1)
seen = [-1] * (N+1)
seen[1] = 0
ans = [0] * (N-1)
while que:
    v = que.pop()
    j = 1
    for u in adj[v]:
        if seen[u] < 0:
            if j == seen[v]:
                j += 1
            seen[u] = j
            ans[edge[(u, v)]] = j
            que.appendleft(u)
            j += 1

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

Solution (Python):

from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N = inp()
lines = defaultdict(set)
ABs = []
for _ in range(N-1):
    a,b = inpl()
    a,b = a-1, b-1
    ABs.append((a,b))
    lines[a].add(b)
    lines[b].add(a)


K = -1
ki = -1
for i in range(N):
    if K < len(lines[i]):
        K = len(lines[i])
        ki = i



colors = [defaultdict(int) for _ in range(N)]

def dfs(s,bs,bk):
    cnt = 0
    for t in lines[s]:
        if t == bs:
            continue
        else:
            if cnt == bk:
                cnt += 1
            colors[s][t] = cnt
            colors[t][s] = cnt
            dfs(t,s,cnt)
            cnt += 1


for i,t in enumerate(lines[ki]):
    colors[ki][t] = i
    colors[t][ki] = i
    dfs(t,ki,i)

print(K)
for a,b in ABs:
    print(colors[a][b]+1)

Solution (Python):
import sys
sys.setrecursionlimit(500000)
N = int(input())
E = [[] for _ in range(N+1)]
for i in range(N-1):
    a, b = map(int, input().split())
    E[a].append((b, i))
    E[b].append((a, i))
K = max(len(e) for e in E)
print(K)
Ans = [-1] * (N-1)
def dfs(v=1, p=0, p_col=-1):
    col = 1
    for u, idx in E[v]:
        if u!=p:
            if col == p_col:
                col += 1
            Ans[idx] = col
            dfs(u, v, col)
            col += 1
dfs()
print("\n".join(map(str, Ans)))