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:
def dfs(tyoten):
    #print(tyoten)
    if(ko[tyoten]==0):
        return
    else:
        for i in range(ko[tyoten]):
            temp=-1
            num=eda[tyoten][i]
            for j in range(m):
                temp=j
                if(not iro[tyoten][j]):
                    iro[tyoten][j]=True
                    saki=ki[tyoten][i]
                    iro[saki][j]=True
                    break
            ans[num]=temp+1
            dfs(ki[tyoten][i])


n=int(input())

ki=[[]for i in range(n)]
eda=[[]for i in range(n)]
ko=[0]*n



for i in range(n-1):
    zen,go=map(int,input().split())
    ki[zen-1].append(go-1)
    eda[zen-1].append(i)

for i in range(n-1):
    ko[i]=len(ki[i])

#print(ki)
#print(eda)
m=max(ko)+1
if(m==ko[0]+1):
    herasu=True
    for i in range(1,n):
        if(m==ko[i]+1):
            herasu=False
    if(herasu):
        m-=1
print(m)


iro=[[False]*m for i in range(n)]
#print(iro)
ans=[-1]*(n-1)
dfs(0)

for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:

N = int(input())
AB = [list(map(int,input().split())) for _ in range(N-1)]
adj = [[] for _ in range(N)]
for i in range(N-1):
    adj[AB[i][0]-1].append([AB[i][1]-1,i])
    adj[AB[i][1]-1].append([AB[i][0]-1,i])


col = [0]*(N-1)
visited = [False] * N
visited[0] = True
def dfs(v, color):
    child = adj[v]
    ctmp = 1
    for u,idx in child:
        if not(visited[u]):
            if ctmp == color:
                ctmp += 1
            col[idx] = ctmp
            visited[u] = True
            dfs(u,ctmp)
            ctmp += 1
    return 
            
    
    
    

dfs(0,0)
K = max(col)
print(K)
for k in col:
    print(k)

Submission status: Runtime Error

Solution code:

L=[{}for i in range(N+1)]
ans=[]
for i in range(N-1):
    a,b=map(int,input().split())
    for j in range(1,N):
        if j not in L[a] and j not in L[b]:
            L[a][j]=1
            L[b][j]=1
            ans.append(j)
            break
print(max(ans))
for i in range(N-1):
    print(ans[i])
#print(L)

Submission status: Runtime Error

Solution code:
N = int(input())
count = [0 for i in range(N)]
n = []
for _ in range(N-1):
  a, b = map(int, input().split())
  count[a-1] += 1
  count[b-1] += 1
  n.append([a, b])

K = max(count)
All = set([j for j in range(1, K+1)])
memo = [[] for i in range(N)]
print(K)
for i in range(N-1):
  a, b = n[i]
  m = list(All - set(memo[a-1] + memo[b-1]))[0]
  memo[a-1].append(m)
  memo[b-1].append(m)
  print(m)

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    mawari = [[] for _ in range(N)]
    e_num = [0] * N 
    for l in range(N-1):
        i, j = [int(x)-1 for x in input().split()]
        e_num[i] += 1
        e_num[j] += 1
        mawari[i].append((j, l))
        mawari[j].append((i, l))
    look = -1
    for ind, l in enumerate(e_num):
        if l == 1:
            look = ind
            break
    color = [-1]*(N-1)
    mc, c = a(N, 0, look, color, mawari)
    print(mc+1)
    for i in c:
        print(i+1)


def a(N, maxc, look, color, mawari):
    nexts = []
    used = []
    for i,j in mawari[look]:
        if color[j] != -1:
            used.append(color[j])
        else:
            nexts.append((i,j))
    c = 0
    for i,j in nexts:
        while c in used:
            c += 1
        color[j] = c
        maxc = max(c, maxc)
        c += 1
    for i,j in nexts:
        maxc = max(maxc, a(N, maxc, i, color, mawari)[0])
    return maxc, color
    

main()

Submission status: Runtime Error

Solution code:
#「辺」彩色
#最大次数で可
#構成はset()使って色管理
#Adいらない
n = int(input())
#Ad = [set() for i in range(n)]
Deg = [0]*n
E = []
for i in range(n-1):
    ai,bi = map(int, input().split( ))
    ai-=1
    bi-=1
    #Ad[ai].add(bi)
    #Ad[bi].add(ai)
    Deg[ai]+=1
    Deg[bi]+=1
    E.append((ai,bi))

cl = max(Deg)
Deg2 = [set(range(cl)) for i in range(n)]
print(cl)
for ai,bi in E:
    #"c = 0
    #while c  not in Deg2[ai] or c not in Deg2[bi]:
    #    #c+=1ここで色探すのが遅かった
    
    for c in Deg2[ai]:
        if c in Deg2[bi]:
            print(c+1)
            break
    Deg2[ai].remove(c)
    Deg2[bi].remove(c)

Submission status: Runtime Error

Solution code:
n = int(input())
edges = [tuple(map(lambda v: int(v) - 1, input().strip().split())) for _ in range(n - 1)]

degrees = [0 for _ in range(n)]
for a, b in edges:
    degrees[a] += 1
    degrees[b] += 1
max_deg = max(degrees)

print(max_deg)

colorset = set(range(max_deg))
used_colors = {v: set() for v in range(n)}

for a, b in edges:
    color = (colorset - (used_colors[a] | used_colors[b])).pop()
    print(color + 1)
    used_colors[a].add(color)
    used_colors[b].add(color)

Submission status: Runtime Error

Solution code:
N = int(input())
cnt_edges = [0]*N
A = []
K = 0
for _ in range(N-1):
    a,b = [int(x) for x in input().split()]
    cnt_edges[a-1]+=1
    cnt_edges[b-1]+=1
    A.append((a,b))

K = max(cnt_edges)

color = [set([i for i in range(1,K+1)]) for _ in range(N)]
print(K)
for edge in A:
    a,b = edge
    col = list(color[a-1] & color[b-1])[0]
    print(col)
    color[a-1].discard(col)
    color[b-1].discard(col)

Submission status: Runtime Error

Solution code:
n = int(input())
edges = [[] for _ in range(n)]
color = [0 for _ in range(n - 1)]
for e in range(n - 1):
    a, b = map(int, input().split())
    edges[a - 1].append((b - 1, e))
    edges[b - 1].append((a - 1, e))

def dfs(i, p, pcol):
    col = 1
    ret = 0
    for c, e in edges[i]:
        if c != p:
            if col == pcol:
                col += 1
            color[e] = col
            ret = max(ret, dfs(c, i, col))
            col += 1
    return max(ret, col - 1)

print(dfs(0, -1, -1))
for i in range(n - 1):
    print(color[i])

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    edges = [[-1]*N for _ in range(N)]
    e_num = [0] * N 
    for l in range(N-1):
        i, j = [int(x)-1 for x in input().split()]
        edges[i][j] = edges[j][i] = l
        e_num[i] += 1
        e_num[j] += 1
    look = -1
    for ind, l in enumerate(e_num):
        if l == 1:
            look = ind
            break
    color = [-1]*(N-1)
    mc, c = a(N, 0, look, color, edges)
    print(mc+1)
    for i in c:
        print(i+1)


def a(N, maxc, look, color, edges):
    nexts = []
    used = []
    for i in range(N):
        if edges[look][i] != -1:
            if color[edges[look][i]] != -1:
                used.append(color[edges[look][i]])
            else:
                nexts.append(i)
    c = 0
    for n in nexts:
        while c in used:
            c += 1
        color[edges[look][n]] = c
        maxc = max(c, maxc)
        c += 1
    for n in nexts:
        maxc = max(maxc, a(N, maxc, n, color, edges)[0])
    return maxc, color
    

main()

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict
readline = sys.stdin.readline
N, = map(int, readline().split())
G = [[] for _ in range(N+1)]
ees = dict()
for v in range(N-1):
    a, b = map(int, readline().split())
    G[a].append(b)
    G[b].append(a)
    ees[v+1] = tuple([min(a,b), max(a,b)])
cd = dict()
cd[1] = 0
ed = dict()

def itree(v, p):
    cdd = 1
    for c in G[v]:
        if c == p:
            continue
        if cdd == cd[v]:
            cdd += 1
        cd[c] = cdd
        ed[tuple([min(c,v), max(c,v)])] = cdd
        cdd += 1
        itree(c, v)


def process():
    itree(1,0)
process()
xs = []
for i in range(N-1):
    xs.append(ed[ees[i+1]])
print(max(xs))
print("\n".join(map(str,xs)))

Submission status: Runtime Error

Solution code:
n = int(input())
edge = [[] for i in range(n)]
a = [0] * n
b = [0] * n

color_dict = {}
k = 0

def dfs(x, last = -1, ban_color = -1):
    global k
    color = 1
    for to in edge[x]:
        if to == last:continue
        if color == ban_color:color += 1
        color_dict[(x, to)] = color
        dfs(to, x, color)
        color += 1
    k = max(k, color-1)

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


dfs(0)
print(k)

for i in range(n-1):
    if (a[i], b[i]) in color_dict:
        print(color_dict[(a[i], b[i])])
    else:
        print(color_dict[(b[i], a[i])])

Submission status: Runtime Error

Solution code:
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')

Submission status: Runtime Error

Solution code:
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])

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict
readline = sys.stdin.readline
N, = map(int, readline().split())
G = [[] for _ in range(N+1)]
for v in range(N-1):
    a, b = map(int, readline().split())
    G[a].append(b)
    G[b].append(a)
cd = dict()
cd[1] = 1

def itree(v, p):
    cdd = 1
    for c in G[v]:
        if c == p:
            continue
        if cdd == cd[v]:
            cdd += 1
        cd[c] = cdd
        cdd += 1
        itree(c, v)


def process():
    itree(1,0)
process()
xs = []
for i in range(N):
    xs.append(cd[i+1])
print(max(xs))
print("\n".join(map(str,xs)))