import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



n = int(readline())

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

gragh = [[] for j in range(n + 1)]

for a, b in ab:

    gragh[a].append(b)

    gragh[b].append(a)

root = 1

parent = [0] * (n + 1)

stack = [root]

order = []



while stack:

    x = stack.pop()

    order.append(x)

    for y in gragh[x]:

        if y == parent[x]:

            continue

        parent[y] = x

        stack.append(y)

color = [-1] * (n + 1)



for x in order:

    ng = color[x]

    cnt = 1

    for y in gragh[x]:

        if y == parent[x]:

            continue

        if cnt == ng:

            cnt += 1

        color[y] = cnt

        cnt += 1



ans = []

append = ans.append

for a, b in ab:

    if parent[a] == b:

        append(color[a])

    else:

        append(color[b])

print((max(ans)))

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

import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

sys.setrecursionlimit(10 ** 7)



from collections import deque



n = int(readline())

graph = [[] for _ in range(n + 1)]

for i in range(n - 1):

    a, b = list(map(int, readline().split()))

    graph[a].append((b, i))

    graph[b].append((a, i))

color = [-1] * (n + 1)

color[1] = -2

ans = [0] * (n - 1)



def bfs(s):

    stack = deque([s])

    while stack:

        num = 1

        now = stack.pop()

        for next, idx in graph[now]:

            if color[next] != -1:

                continue

            if color[now] == num:

                num += 1

            color[next] = num

            ans[idx] = num

            num += 1

            stack.append(next)





bfs(1)

print((max(ans)))

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

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n = int(readline())
ab = [list(map(int, readline().split())) for i in range(n - 1)]
gragh = [[] for j in range(n + 1)]
for a, b in ab:
    gragh[a].append(b)
    gragh[b].append(a)
root = 1
parent = [0] * (n + 1)
stack = [root]
order = []
while stack:
    x = stack.pop()
    order.append(x)
    for y in gragh[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        stack.append(y)
color = [-1] * (n + 1)
for x in order:
    ng = color[x]
    cnt = 1
    for y in gragh[x]:
        if y == parent[x]:
            continue
        if cnt == ng:
            cnt += 1
        color[y] = cnt
        cnt += 1
ans = []
append = ans.append
for a, b in ab:
    if parent[a] == b:
        append(color[a])
    else:
        append(color[b])
print((max(ans)))
print(("\n".join(map(str, ans))))

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)
from collections import deque

n = int(readline())
graph = [[] for _ in range(n + 1)]
for i in range(n - 1):
    a, b = list(map(int, readline().split()))
    graph[a].append((b, i))
    graph[b].append((a, i))
color = [-1] * (n + 1)
color[1] = -2
ans = [0] * (n - 1)


def bfs(s):
    stack = deque([s])
    while stack:
        num = 1
        now = stack.pop()
        for next, idx in graph[now]:
            if color[next] != -1:
                continue
            if color[now] == num:
                num += 1
            color[next] = num
            ans[idx] = num
            num += 1
            stack.append(next)


bfs(1)
print((max(ans)))
print(("\n".join(map(str, ans))))

+sys.setrecursionlimit(10**7) +from collections import deque + -ab = [list(map(int, readline().split())) for i in range(n - 1)] -gragh = [[] for j in range(n + 1)] -for a, b in ab: -    gragh[a].append(b) -    gragh[b].append(a) -root = 1 -parent = [0] * (n + 1) -stack = [root] -order = [] -while stack: -    x = stack.pop() -    order.append(x) -    for y in gragh[x]: -        if y == parent[x]: -            continue -        parent[y] = x -        stack.append(y) +graph = [[] for _ in range(n + 1)] +for i in range(n - 1): +    a, b = list(map(int, readline().split())) +    graph[a].append((b, i)) +    graph[b].append((a, i)) -for x in order: -    ng = color[x] -    cnt = 1 -    for y in gragh[x]: -        if y == parent[x]: -            continue -        if cnt == ng: -            cnt += 1 -        color[y] = cnt -        cnt += 1 -ans = [] -append = ans.append -for a, b in ab: -    if parent[a] == b: -        append(color[a]) -    else: -        append(color[b]) +color[1] = -2 +ans = [0] * (n - 1) + + +def bfs(s): +    stack = deque([s]) +    while stack: +        num = 1 +        now = stack.pop() +        for next, idx in graph[now]: +            if color[next] != -1: +                continue +            if color[now] == num: +                num += 1 +            color[next] = num +            ans[idx] = num +            num += 1 +            stack.append(next) + + +bfs(1)