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: Wrong Answer

Solution code:
N = int(input())
A,B = [],[]
for _ in range(N-1):
    array = list(map(int,input().split()))
    A.append(array[0])
    B.append(array[1])
from collections import defaultdict as ddict
children = ddict(list)
for a,b in zip(A,B):
    children[a].append(b)
ans = ddict(int)
col = 0
for child in sorted(children.keys()):
    used = set({ans[child]})
    for j in sorted(children[child]):
        temp = min(set(range(1,max(used)+2))-used)
        ans[j] = temp
        used.add(temp)
ans2 = (ans[b] for b in B)
print(max(ans2))
for a in ans2:
    print(a)

Submission status: Wrong Answer

Solution code:
N = int(input())
A,B = [],[]
for _ in range(N-1):
    array = list(map(int,input().split()))
    A.append(array[0])
    B.append(array[1])
from collections import defaultdict as ddict
children = ddict(list)
for a,b in zip(A,B):
    children[a].append(b)
ans = ddict(int)
col = 0
for child in sorted(children.keys()):
    used = set({1,ans[child]})
    for j in sorted(children[child]):
        temp = min(set(range(1,max(used)+2))-used)
        ans[j] = temp
        col = max(col,temp)
        used.add(temp)
print(col)
print("\n".join(str(ans[b]) for b in B))

Submission status: Wrong Answer

Solution code:
N = int(input())
A,B = [],[]
for _ in range(N-1):
    array = list(map(int,input().split()))
    A.append(array[0])
    B.append(array[1])
from collections import defaultdict as ddict
children = ddict(list)
for a,b in zip(A,B):
    children[a].append(b)
ans = ddict(int)
col = 0
for child in sorted(children.keys()):
    used = set({0,ans[child]})
    for j in sorted(children[child]):
        temp = min(set(range(max(used)+2))-used)
        ans[j] = temp
        col = max(col,temp)
        used.add(temp)
print(col)
ans2 = [ans[b] for b in B]
print(*ans2,sep="¥n")

Submission status: Wrong Answer

Solution code:
N = int(input())
A,B = [],[]
for _ in range(N-1):
    array = list(map(int,input().split()))
    A.append(array[0])
    B.append(array[1])
from collections import defaultdict as ddict
children = ddict(list)
for a,b in zip(A,B):
    children[a].append(b)
ans = ddict(int)
col = 0
for child in sorted(children.keys()):
    used = set({ans[child]})
    for j in sorted(children[child]):
        temp = min(set(range(1,max(used)+2))-used)
        ans[j] = temp
        col = max(col,temp)
        used.add(temp)
ans2 = [ans[b] for b in B]
print(*ans2, sep='\n')

Submission status: Wrong Answer

Solution code:
N = int(input())
AB = []
for _ in range(N-1):
    AB.append([int(i) for i in input().split()])
from collections import defaultdict as ddict
children = ddict(list)
parents = ddict(list)
for ab in AB:
    children[ab[0]].append(ab[1])
    parents[ab[1]].append(ab[0])
ans = []
ans_trans = ddict(int)
for i in range(N-1):
    used = set()
    # 親チェック
    if parents[AB[i][0]] != []:
        used.add(ans_trans[str(parents[AB[i][0]][0]) + "-" + str(AB[i][0])])
#     # 子チェック
#     for child in children[AB[i][1]]:
#         used.add(ans_trans[str(AB[i][1])+"-"+str(child)])
    # 兄弟チェック
    for child in children[AB[i][0]]:
        used.add(ans_trans[str(AB[i][0])+"-"+str(child)])
    ans.append(min(set(range(1,max(used)+2)) - used))
    ans_trans[str(AB[i][0]) + "-" + str(AB[i][1])] = ans[-1]
print(max(ans))
for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

def main():
    n = int(sys.stdin.readline().rstrip())
    ab = map(int, sys.stdin.read().split())
    ab = list(zip(ab, ab))

    G = [[] for _ in range(n + 1)]
    for a, b in ab:
        G[a].append(b)
        G[b].append(a)
    
    q = deque([1]) # root = 1
    parent = [None] * (n + 1)
    color = [0] * (n + 1) # color of the edge(parent[y], y)

    while q:
        x = q.popleft()
        ng = color[x]
        c = 1
        for y in G[x]:
            if y == parent[x]:
                continue
            parent[y] = x
            if c == ng:
                c += 1
            color[y] = c
            q.append(y)
            c += 1
            
    k = max(color)

    for a, b in ab:
        if a == parent[b]:
            print(color[b])
        else:
            print(color[a])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
AB = []
for _ in range(N-1):
    AB.append([int(i) for i in input().split()])
from collections import defaultdict as ddict
children = ddict(list)
parents = ddict(list)
for ab in AB:
    children[ab[0]].append(ab[1])
    parents[ab[1]].append(ab[0])
ans = []
ans_trans = ddict(int)
for i in range(N-1):
    used = set()
    # 親チェック
    if parents[AB[i][0]] != []:
        used.add(ans_trans[str(parents[AB[i][0]][0]) + "-" + str(AB[i][0])])
    # 子チェック
    for child in children[AB[i][1]]:
        used.add(ans_trans[str(AB[i][1])+"-"+str(child)])
    # 兄弟チェック
    for child in children[AB[i][0]]:
        used.add(ans_trans[str(AB[i][0])+"-"+str(child)])
    ans.append(min(set(range(1,max(used)+2)) - used))
    ans_trans[str(AB[i][0]) + "-" + str(AB[i][1])] = ans[-1]
print(max(ans))
for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

def main():
    n = int(sys.stdin.readline().rstrip())
    ab = map(int, sys.stdin.read().split())
    ab = list(zip(ab, ab))

    G = [[] for _ in range(n + 1)]
    for a, b in ab:
        G[a].append(b)
        G[b].append(a)
    
    root = 1
    q = deque()
    q.append(root)
    parent = [None] * (n + 1)
    color = [0] * (n + 1) # color[y]はx = parent[y]としてcolor[x] に依存する。

    while q:
        x = q.popleft()
        c = 1
        for y in G[x]:
            if y == parent[x]:
                continue
            parent[y] = x
            if color[x] == c:
                c += 1
            color[y] = c
            c += 1
            q.append(y)

    k = max(color)
    print(k)
    print('\n'.join(map(str, color[2:])))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

def main():
    n = int(sys.stdin.readline().rstrip())
    ab = map(int, sys.stdin.read().split())
    ab = list(zip(ab, ab))

    G = [[] for _ in range(n + 1)]
    for a, b in ab:
        G[a].append(b)
        G[b].append(a)
    
    root = 1
    q = deque()
    q.append(root)
    parent = [None] * (n + 1)
    color = [0] * (n + 1) # color[y]はx = parent[y]としてcolor[x] に依存する。
    
    while q:
        x = q.popleft()
        c = 1
        for y in G[x]:
            if y == parent[x]:
                continue
            parent[y] = x
            if color[x] == c:
                c += 1
            color[y] = c
            q.append(y)

    k = max(color)
    print(k)
    print('\n'.join(map(str, color[2:])))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

def main():
    n = int(sys.stdin.readline().rstrip())
    ab = map(int, sys.stdin.read().split())
    ab = list(zip(ab, ab))

    G = [[] for _ in range(n + 1)]
    for a, b in ab:
        G[a].append(b)
        G[b].append(a)
    
    root = 1
    q = deque()
    q.append(root)
    parent = [None] * (n + 1)
    color = [0] * (n + 1) # color[y]はx = parent[y]としてcolor[x] に依存する。
    print(color)
    while q:
        x = q.popleft()
        c = 1
        for y in G[x]:
            if y == parent[x]:
                continue
            parent[y] = x
            if color[x] == c:
                c += 1
            color[y] = c
            q.append(y)

    k = max(color)
    print(k)
    print('\n'.join(map(str, color[2:])))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
 
tree_dict = {}
tree_lst = []
 
for i in range(N-1):
    a, b = map(int, input().split())
    tree_lst.append([a, b])
    if a in tree_dict:
        tree_dict[a].append(b)
    else: tree_dict[a] = [b]
    if b in tree_dict:
        tree_dict[b].append(a)
    else: tree_dict[b] = [a]
 
colors = -1
      
for val in tree_dict.values():
    colors = max(colors, len(val))
 
bond = []
[bond.append([0]*len(tree_dict[i])) for i in range(1,N + 1) if i in tree_dict]
 
for a,b in tree_lst:
    for i in range(1, colors + 1):
        if i in bond[a-1]:continue
        elif len(bond[a-1]) < i:break
        else:
            for ni, bi in enumerate(tree_dict[a]):
                if bi == b:bond[a-1][ni] = i
            for nj, bj in enumerate(tree_dict[b]):
                if bj == a:bond[b-1][nj] = i
 
print(colors)
for a, b in tree_lst:
    for ni, bi in enumerate(tree_dict[a]):
        if bi == b:print(bond[a-1][ni])

Submission status: Wrong Answer

Solution code:
N = int(input())

tree_dict = {}
tree_lst = []

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

colors = -1
      
for val in tree_dict.values():
    colors = max(colors, len(val))

bond = []
[bond.append([0]*len(tree_dict[i])) for i in range(1,N + 1) if i in tree_dict]

for a,b in tree_lst:
    for i in range(1, colors + 1):
        if i in bond[a-1]:continue
        elif len(bond[a-1]) < i:break
        else:
            for ni, bi in enumerate(tree_dict[a]):
                if bi == b:bond[a-1][ni] = i
            for nj, bj in enumerate(tree_dict[b]):
                if bj == a:bond[b-1][nj] = i
            print(i, bond)

print(colors)
for a, b in tree_lst:
    for ni, bi in enumerate(tree_dict[a]):
        if bi == b:print(bond[a-1][ni])

Submission status: Wrong Answer

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

N = int(input())
ki =[[] for _ in range(N)]
colorLst = [[] for _ in range(N - 1)]
ansLst = [0] * (N - 1)

def dfs(v, p = 0, ban_color = 0):
    color = 1
    for i in ki[v]:
        if i == p:
            continue
        if ban_color == color:
            color += 1
        colorLst[v].append(color)
        dfs(i, v, color)
        color += 1
    return


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

dfs(0)

k = 0

for i in range(len(colorLst)):
    if colorLst[i] != []:
        k = max(k, max(colorLst[i]))
print(k)
for i in range(N - 1):
    print(colorLst[ansLst[i]].pop())

Submission status: Wrong Answer

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

N = int(input())
ki =[[] for _ in range(N)]
colorLst = [[] for _ in range(N - 1)]
ansLst = [0] * (N - 1)

def dfs(v, p = 0, ban_color = 0):
    color = 1
    for i in ki[v]:
        if i == p:
            continue
        if ban_color == color:
            color += 1
        colorLst[v].append(color)
        dfs(i, v, color)
        color += 1
    return


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

dfs(0)

k = 0
if len(ki[0]) == N - 1:
    k = N - 1
else:
    for i in range(len(colorLst)):
        k = max(k, len(colorLst[i]) + 1)
print(k)
for i in range(N - 1):
    print(colorLst[ansLst[i]].pop())

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
input = sys.stdin.readline
N = int(input())
children = [[] for _ in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    children[a-1].append(b-1)

queue = deque([0])
colors = [-1] * N
maxColor = 0
while queue:
    node = queue.popleft()
    nodeColor = colors[node]
    color = 1
    for child in children[node]:
        if color == nodeColor: color += 1
        colors[child] = color
        queue.append(child)
        color += 1
    maxColor = max(maxColor, color-1)

print(maxColor)
for i in range(1,N):
    print(colors[i])

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
input = sys.stdin.readline
N = int(input())
children = [[] for _ in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    children[a-1].append(b-1)

queue = deque([0])
colors = [0] * N
maxColor = 0
while queue:
    node = queue.popleft()
    nodeColor = colors[node]
    color = 1
    for child in children[node]:
        if color == nodeColor: color += 1
        colors[child] = color
        queue.append(child)
        color += 1
    maxColor = max(maxColor, color-1)

print(maxColor)
for i in range(1,N):
    print(colors[i])

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
#MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
#import numpy as np
# import operator
# import bisect
# from heapq import heapify,heappop,heappush
#from math import gcd
# from fractions import gcd
from collections import deque
# from collections import defaultdict
# from collections import Counter
#from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


def main():
	n=int(input())
	E=[[] for i in range(n+1)]
	for i in range(n-1):
		a,b=map(int,input().split())
		E[a].append(b)
	C=[0]*(n+1)
	q = deque([1])
	while q:
		now = q.popleft()
		cnt=1
		for i in E[now]:
			if cnt==C[now]:
				cnt+=1
			C[i]=cnt
			cnt+=1
			q.append(i)
	C=C[2:]
	print(max(C))
	print('\n'.join(map(str,C)))
	
if __name__ == "__main__":
		main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
#MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
#import numpy as np
# import operator
# import bisect
# from heapq import heapify,heappop,heappush
#from math import gcd
# from fractions import gcd
from collections import deque
# from collections import defaultdict
# from collections import Counter
#from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


def main():
	n=int(input())
	li=[[] for i in range(n+1)]
	sumi = [[] for i in range(n + 1)]
	for i in range(n-1):
		a,b=map(int,input().split())
		li[a].append(b)
	ans=[]
	ans2=[]
	
	q = deque()
	q.append(1)
	while q:
		now = q.popleft()
		cnt=1
		for i in li[now]:
			q.append(i)
			while cnt in sumi[i-1]:
				cnt+=1
			ans.append((now,i,cnt,sumi))
			ans2.append(cnt)
			sumi[i].append(cnt)
			
	print(max(ans2))
	print('\n'.join(map(str,ans2)))
	
if __name__ == "__main__":
		main()

Submission status: Wrong Answer

Solution code:
import sys

from collections import defaultdict

sys.setrecursionlimit(20000000)

class G:
    def __init__(self):
        self.edges = defaultdict(list)
        self.N = int(input())
        self.edge_numbers = {}
        for i in range(self.N - 1):
            a, b = map(int, input().split())
            self.edges[a - 1].append(b - 1)
            self.edges[b - 1].append(a - 1)
            self.edge_numbers[i] = (a - 1, b - 1)

        self.K = max(len(self.edges[v]) for v in self.edges.keys())

        self.visited = set()
        self.levels = defaultdict(int)
        self.painted = defaultdict(int)

    def paint(self, v, color, level):
        print(v)
        if v in self.visited:
            return
        self.levels[v] = level
        self.visited.add(v)

        paint_color = 1
        for e in self.edges[v]:
            if e in self.visited:
                continue
            if paint_color == color:
                paint_color += 1
            self.painted[e] = paint_color
            self.paint(e, paint_color, level + 1)
            paint_color += 1


    def print(self):
        print(self.K)
        for i in range(len(self.edge_numbers)):
            a, b = self.edge_numbers[i]
            color = -1
            if self.levels[a] < self.levels[b]:
                color = self.painted[b]
            else:
                color = self.painted[a]
            print(color)

g = G()
g.paint(0, 0, 0)
g.print()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

from collections import deque

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

tree = {}
node_in = [0 for i in range(n)] # 頂点に入る辺は1つ
node_out = [[0] for i in range(n)] # 頂点から出る辺は1つ以上
for edge in sorted(edges):
    a = edge[0] # 頂点a
    b = edge[1] # 頂点b

    color = 0
    if node_out[a-1][-1] == 0:
        if node_in[a-1] <= 1: # node_in[a-1] == 0の時1、1の時2なのでまとめて
            color = node_in[a-1] + 1
        else: # node_in[a-1] >= 2
            color = 1
    else:
        color = node_out[a-1][-1] + 1
        if node_in == color:
            color += 1
            
    tree[(a, b)] = color
    node_out[a-1].append(color)
    node_in[b-1] = color

print(max(tree.values()))
for edge in edges:
    a = edge[0] # 頂点a
    b = edge[1] # 頂点b
    print(tree[(a, b)])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

from collections import deque

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

tree = {}
node_in = [0 for i in range(n)] # 頂点に入る辺は1つ
node_out = [[0] for i in range(n)] # 頂点から出る辺は1つ以上
for edge in sorted(edges):
    a = edge[0] # 頂点a
    b = edge[1] # 頂点b

    color = 0
    if node_out[a-1][-1] == 0:
        if node_in[a-1] <= 1: # node_in[a-1] == 0の時1、1の時2なのでまとめて
            color = node_in[a-1] + 1
        else: # node_in[a-1] >= 2
            color = 1
    else:
        color = node_out[a-1][-1] + 1
        if node_in == color:
            color += 1
            
    tree[(a, b)] = color
    node_out[a-1].append(color)
    node_in[b-1] = color

for edge in edges:
    a = edge[0] # 頂点a
    b = edge[1] # 頂点b
    print(tree[(a, b)])

Submission status: Wrong Answer

Solution code:
N = int(input())

M = [set() for _ in range(N)]

A = []
ans = []
for _ in range(N - 1):
    A.append(list(map(int, input().split())))
A = sorted(A)
for a in A:
    cnt = 1
    while(cnt in M[a[0] - 1] or cnt in M[a[1] - 1]):
            cnt += 1
    M[a[0] - 1].add(cnt)
    M[a[1] - 1].add(cnt)
    ans.append(cnt)

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

Submission status: Wrong Answer

Solution code:
import queue

q = queue.Queue()

N = int(input())

M = [set() for _ in range(N)]

A = []
ans = []
for _ in range(N - 1):
    A.append(list(map(int, input().split())))
for a in A:
    cnt = 1
    while(cnt in M[a[0] - 1] or cnt in M[a[1] - 1]):
            cnt += 1
    M[a[0] - 1].add(cnt)
    M[a[1] - 1].add(cnt)
    ans.append(cnt)

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

Submission status: Wrong Answer

Solution code:
from collections import deque
n = 100
vs = [[] for i in range(n)]
out = [0]*n

for i in range(n-1):
    a, b = [i+0,i+1]
    vs[a].append([b,i+1])
    vs[b].append([a,i+1])

nex = [[0,-1]]

def coloring(index,defColor):
    i=0
    for v in vs[index]:
        if i==defColor:
           i+=1
        out[v[1]]=i
        vs[v[0]].remove([index,v[1]])
        nex.append([v[0],i])
        i+=1
    out[0] = max(out[0],i-1)


while nex.__len__()>0:
    c,d = nex.pop()
    coloring(c,d)

for i in range(n):
    print(out[i]+1)

Submission status: Wrong Answer

Solution code:
N = int(input())

# M = [[] for _ in range(N)]
# A = [[] for _ in range(N)]
# for _ in range(N - 1):
#     aa, bb = map(int, input().split())
#     M[aa - 1].append(bb - 1)
#     M[bb - 1].append(aa - 1)

M = []
A = [[] for _ in range(N)]
for _ in range(N - 1):
    M.append(list(map(int, input().split())) + [0])

A = [[] for _ in range(N)]

MAX = -1
for m in M:
    cnt = 1
    while cnt in A[m[0] - 1] + A[m[1] - 1]:
        cnt += 1
    A[m[0] - 1].append(cnt)
    A[m[1] - 1].append(cnt)
    m[2] = cnt

    if MAX < cnt:
        MAX = cnt

print(MAX)
for n in range(N - 1):
    print(M[n][2])

Submission status: Wrong Answer

Solution code:
n=int(input())

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

    k=int(0)
    while True:
        if (k in l[a])==False:
            if (k in l[b])==False:
                l[a].append(k)
                l[b].append(k)
                break
            else:
                k +=1
        else:
            k +=1

    t.append(k+1)

print(max(t))
for g in range(n-1):
    print(t[g])

Submission status: Wrong Answer

Solution code:
n=int(input())

l=[[] for j in range(n)]
m=[e for e in range(n-1)]
t=[]

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

    buf = min(list(set(m)^set(l[a]) ^ set(l[b])))
    l[a].append(buf)
    l[b].append(buf)
    """
    while True:
        if (k in l[a])==False and (k in l[b])==False:
            l[a].append(k)
            l[b].append(k)
            break
        else:
            k +=1
"""
    t.append(buf+1)

print(max(t))
for g in range(n-1):
    print(t[g])

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]
w=[]
r2=[]
r1=[]
r3=[]
e=[]
t=int(1)
for i in range(n-1):
    a, b = map(int, input().split())
    if a ==1:
        w.append([a,b])
        r2.append([i,t])
        t +=1

    else:
        l.append([a,b])
        r1.append([i,0])
def cal(r1,r2,l,w,c):
    while True:
        if l==[]:
            return r2,c
        t=1
        i=0
        while True:
            if (w[0][0] in l[i])==True or (w[0][1] in l[i])==True:
                w.insert(1,l[i])
                l.pop(i)
                r2.append(r2[0])
                if not r2[0][1]==t:
                    r1[0][1]=t
                else:
                    t +=1
                    r1[0][1]=t
                c=max(c,t)
                r2.insert(1,r1[0])
                r1.pop(0)
                r2.pop(0)
                t +=1
            else:
                i +=1

            if not len(l)>i:
                i=0
                w.pop(0)
                break
c=t-1

r3,c=cal(r1,r2,l,w,c)
e=sorted(r3)

print(c)
for g in range(n-1):
    print(e[g][1])

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]
w=[]
r2=[]
r1=[]
r3=[]
e=[]
t=int(1)
for i in range(n-1):
    a, b = map(int, input().split())
    if a ==1:
        w.append([a,b])
        r2.append([i,t])
        t +=1

    else:
        l.append([a,b])
        r1.append([i,0])
def cal(r1,r2,l,w,c):
    while True:
        if l==[]:
            return r2,c
        t=1
        i=0
        while True:
            if (w[0][0] in l[i])==True or (w[0][1] in l[i])==True:
                w.insert(1,l[i])
                l.pop(i)
                r2.append(r2[0])
                if not r2[0][1]==t:
                    r1[0][1]=t
                else:
                    t +=1
                    r1[0][1]=t
                c=max(c,t)
                r2.insert(1,r1[0])
                r1.pop(0)
                r2.pop(0)
                t +=1
            else:
                i +=1

            if not len(l)>i:
                i=0
                w.pop(0)
                break
c=0

r3,c=cal(r1,r2,l,w,0)
e=sorted(r3)

print(c)
for g in range(n-1):
    print(e[g][1])

Submission status: Wrong Answer

Solution code:
N = int(input())
AB = []
for _ in range(N-1):
    AB.append([int(i) for i in input().split()])
from collections import defaultdict as ddict
tree_children = ddict(set)
tree_parent = ddict(int)
for ab in AB:
    tree_children[ab[0]].add(ab[1])
    tree_parent[ab[1]] = ab[0]

ans = [0] * (N-1)
for i in range(2,N+1):
    relations = tree_children[i] ^ set([tree_parent[i]])  ^ tree_children[tree_parent[i]]
    used = set()
    for r in relations:
        if r != 1 and  ans[r-2] != 0:
            used.add(ans[r-2])
    ans[i-2] = 1 if used==set() else min(set(range(1,max(used)+2)) - used)
    
print(max(ans))
for a in ans:
  print(a)

Submission status: Wrong Answer

Solution code:
import collections
n = int(input())
al = []
bl = []
cl = []
ngl = []
for i in range(n-1):
    a,b = map(int,input().split())
    for i,ai in enumerate(al,0):
        if ai == a or ai == b:
            ngl.append(cl[i])
    for i,bi in enumerate(bl,0):
        if bi == b or bi == a:
            ngl.append(cl[i])
    al.append(a)
    bl.append(b)
    if ngl == []:
        cl.append(1)
    if ngl != []:
        for i,ng in enumerate(set(ngl),1):
            if ng != i:
                cl.append(i)
                ngl = []
                break
    if ngl != []:
        m = max(ngl)
        cl.append((max(ngl)+1))
    ngl = []
print(len(collections.Counter(cl)))
for i in range(len(cl)):
    print(cl[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
p = []
for i in range(n-1):
    p.append(list(map(int, input().split())))
color = [0 for i in range(n-1)]
Ncolor = [[] for i in range(n)]
maxcolor = 1
for i, value in enumerate(p):
    color[i] = 1
    l = value[0]-1
    r = value[1]-1
    for j in range(100001):
        if color[i] in Ncolor[l]:
            color[i] = color[i] + 1
        elif color[i] in Ncolor[r]:
            color[i] = color[i] + 1
        else:
            Ncolor[l].append(color[i])
            Ncolor[r].append(color[i])
            if maxcolor < color[i]:
                maxcolor = int(color[i])
            break
print(maxcolor)
for i in color:
    print(i)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter


def main():
    N = int(input())
    A = []
    B = []
    C = []
    for _ in range(N - 1):
        a, b = [int(x) for x in input().split()]
        A.append(a)
        B.append(b)
        C.append(a)
        C.append(b)
    
    c = Counter(C)
    memo = Counter()
    print(c.most_common(1)[0][1])

    for i in range(N - 1):
        x = memo[A[i]] | memo[B[i]]
        if x == 0:
            memo[A[i]] += 1
            memo[B[i]] += 1
            print(1)
            continue

        l = len(bin(x)[2:])
        j = bin(x)[2:].find('0')
        if j == -1:
            memo[A[i]] += 2 ** l
            memo[B[i]] += 2 ** l
            print(l + 1)
        else:
            memo[A[i]] += 2 ** (l - j - 1)
            memo[B[i]] += 2 ** (l - j - 1)
            print(l - j)



    

    



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter


def main():
    N = int(input())
    A = []
    B = []
    C = []
    for _ in range(N - 1):
        a, b = [int(x) for x in input().split()]
        A.append(a)
        B.append(b)
        C.append(a)
        C.append(b)
    
    c = Counter(C)
    memo = Counter()
    print(c.most_common(1)[0][1])

    for i in range(N - 1):
        x = memo[A[i]] | memo[B[i]]
        if x == 0:
            memo[A[i]] += 1
            memo[B[i]] += 1
            print(1)
            continue

        l = len(bin(x)[2:])
        j = bin(x)[2:].find('0')
        if j == -1:
            memo[A[i]] += 2 ** l
            memo[B[i]] += 2 ** l
            print(l + 1)
        else:
            memo[A[i]] = 2 ** (l - j - 1)
            memo[B[i]] = 2 ** (l - j - 1)
            print(l - j)



    

    



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())

v=[ [] for _ in range(n)]
edge=[]
next_edge=[]

for i in range(n-1):
    a,b=map(int,input().split())
    v[a-1].append(i)
    v[b-1].append(i)
    #隣接辺の数,インデックス,色,結んでいる頂点
    edge.append([0,i,0,[a-1,b-1]])

for i in range(n-1):
    ei=edge[i]
    edge[i][0]=len(v[ei[3][0]])+len(v[ei[3][1]])-2
    next_ei=[]
    next_ei.extend(v[ei[3][0]])
    next_ei.extend(v[ei[3][1]])
    next_ei=list(set(next_ei))
    next_ei.remove(i)
    next_edge.append(next_ei)

sorted_edge = sorted(edge,reverse=True)

max_color=1
for i in range(len(sorted_edge)):
    ei=sorted_edge[i]
    next_ei = next_edge[ei[1]]
    next_color=[]
    for j in next_ei:
        next_color.append(edge[j][2])
    next_color=list(set(next_color))
    color=1
    while(color in next_color):
        color+=1

    sorted_edge[i][2]=color
    edge[ei[1]][2]=color
    if(max_color < color):
        max_color=color


print(max_color)
for i in range(n-1):
    print(edge[i][2])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter


def main():
    N = int(input())
    A = []
    B = []
    C = []
    for _ in range(N - 1):
        a, b = [int(x) for x in input().split()]
        A.append(a)
        B.append(b)
        C.append(a)
        C.append(b)
    
    c = Counter(C)
    memo = Counter()
    print(c.most_common(1)[0][1])

    for i in range(N - 1):
        memo[A[i]] += 1
        memo[B[i]] += 1
        if memo[A[i]] == memo[B[i]]:
            print(memo[A[i]])
        elif memo[A[i]] > memo[B[i]]:
            print(memo[A[i]])
            memo[B[i]] -= 1
        else:
            print(memo[B[i]])
            memo[A[i]] -= 1

    

    



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter


def main():
    N = int(input())
    A = []
    B = []
    C = []
    for _ in range(N - 1):
        a, b = [int(x) for x in input().split()]
        A.append(a)
        B.append(b)
        C.append(a)
        C.append(b)
    
    c = Counter(C)
    memo = Counter()
    print(c.most_common(1)[0][1])

    for i in range(N - 1):
        memo[A[i]] += 1
        memo[B[i]] += 1
        print(max(memo[A[i]], memo[B[i]]))
    

    



if __name__ == '__main__':
    main()