Problem p02850 (python)

Fixed submission:
from collections import deque

N=int(input())
a=[0]*(N-1)
b=[0]*(N-1)
c=[set() for _ in range(N)] # 使用している色
d=[set() for _ in range(N)] # 隣接リスト
e=[ False for _ in range(N)] #訪問ずみかどうか
f=[0]*N  #答え
for i in range(N-1):
    a[i],b[i] = map(int,input().split())
    d[a[i]-1].add(b[i]-1)
    d[b[i]-1].add(a[i]-1)

maxi=max([len(d[i]) for i in range(N-1)])
print(maxi)
#print("c:",c)
#print("d",d)
#print("e",e)

#x=0としてBFSする

v=deque([0])
e[0]=True
color=1

while v:
  p = v.popleft()
  for q in d[p]:
    if e[q]==False:
      while color in c[p]:
        color +=1
        if color>maxi:
          color=1
      c[p].add(color)
      c[q].add(color)
      f[q]=color
      e[q]=True
      v.append(q)
      
for i in range(N-1):
  print( f[b[i]-1])

Buggy submission (verdict: Runtime Error):
N=int(input())
a=[0]*(N-1)
b=[0]*(N-1)
c=[0]*(N-1)
d=[[] for _ in range(N)] # 隣接リスト
e=[ set() for _ in range(N)] #その頂点につながる辺に存在する色のリスト
maxi=0
for i in range(N-1):
    a[i],b[i] = map(int,input().split())
    d[a[i]-1].append(b[i]-1)
    d[b[i]-1].append(a[i]-1)
    
for j in d:
    maxi=max(maxi,len(j))  #最大の色数が確定する

possible=set( i+1 for i in range(maxi) )

for i in range(N-1):
  tmp=set()
  tmp |= possible
  tmp -= e[a[i]-1]
  tmp -= e[b[i]-1]
  c[i]=tmp.pop()
  e[a[i]-1].add(c[i])
  e[b[i]-1].add(c[i])
   
print(maxi)
for color in c:
    print(color)

Buggy submission (verdict: Runtime Error):
N=int(input())
a=[0]*(N-1)
b=[0]*(N-1)
c=[0]*(N-1)
d=[[] for _ in range(N)] # 隣接リスト
e=[ set() for _ in range(N)] #その頂点につながる辺に存在する色のリスト
maxi=0
for i in range(N-1):
    a[i],b[i] = map(int,input().split())
    d[a[i]-1].append(b[i]-1)
    d[b[i]-1].append(a[i]-1)
    
for j in d:
    maxi=max(maxi,len(j))  #最大の色数が確定する
pos=set([i+1 for i in range(maxi)])

for i in range(N-1):
    tmp= pos- e[a[i]-1] -e[b[i]-1]
    col=tmp.pop()
    c[i]=col
    e[a[i]-1].add(col)
    e[b[i]-1].add(col)
   
print(maxi)
for color in c:
    print(color)

Buggy submission (verdict: Runtime Error):
N=int(input())
a=[0]*(N-1)
b=[0]*(N-1)
c=[0]*(N-1)
d=[[] for _ in range(N)] # 隣接リスト
e=[ set() for _ in range(N)] #その頂点につながる辺に存在する色のリスト
maxi=0
for i in range(N-1):
    a[i],b[i] = map(int,input().split())
    d[a[i]-1].append(b[i]-1)
    d[b[i]-1].append(a[i]-1)
    
for j in d:
    maxi=max(maxi,len(j))  #最大の色数が確定する
for i in range(N-1):
    pos=set([i+1 for i in range(maxi)])
    pos= pos- e[a[i]-1] -e[b[i]-1]
    col=pos.pop()
    c[i]=col
    e[a[i]-1].add(col)
    e[b[i]-1].add(col)
   
print(maxi)
for color in c:
    print(color)

Buggy submission (verdict: Runtime Error):
N=int(input())
a=[0]*(N-1)
b=[0]*(N-1)
c=[0]*(N-1)
d=[[] for _ in range(N)] # 接続
e=[ set() for _ in range(N)] #その頂点につながる辺に存在する色のリスト
maxi=0
for i in range(N-1):
    a[i],b[i] = map(int,input().split())
    d[a[i]-1].append(b[i]-1)
    d[b[i]-1].append(a[i]-1)
    
for j in d:
    maxi=max(maxi,len(j))  #最大の色数が確定する
for i in range(N-1):
    pos=set([i+1 for i in range(maxi)])
    pos= pos- e[a[i]-1] -e[b[i]-1]
    col=min(pos)
    c[i]=col
    e[a[i]-1].add(col)
    e[b[i]-1].add(col)
   
print(maxi)
for color in c:
    print(color)

Buggy submission (verdict: Runtime Error):
N=int(input())
a=[0]*(N-1)
b=[0]*(N-1)
c=[0]*(N-1)
d=[[] for _ in range(N)] # 接続
e=[ set() for _ in range(N)] #その頂点につながる辺に存在する色のリスト
maxi=0
for i in range(N-1):
    a[i],b[i] = map(int,input().split())
    d[a[i]-1].append(b[i]-1)
    d[b[i]-1].append(a[i]-1)
    
for j in d:
    maxi=max(maxi,len(j))  #最大の色数が確定する
for i in range(N-1):
    pos=set([i+1 for i in range(maxi)])
    pos= pos- e[a[i]-1] -e[b[i]-1]
    col=min(pos)
    c[i]=col
    e[a[i]-1].add(col)
    e[b[i]-1].add(col)
   
print(maxi)
for color in c:
    print(color)

Buggy submission (verdict: Runtime Error):
N=int(input())
a=[0]*(N-1)
b=[0]*(N-1)
c=[0]*(N-1)
d=[[] for _ in range(N)]
e=[ set() for _ in range(N)] #存在する色のリスト
maxi=0
for i in range(N-1):
    a[i],b[i] = map(int,input().split())
    d[a[i]-1].append(b[i]-1)
    d[b[i]-1].append(a[i]-1)
for j in d:
    maxi=max(maxi,len(j))
for i in range(N-1):
    pos=set([i+1 for i in range(maxi)])
    pos= pos- e[a[i]-1] -e[b[i]-1]
    col=min(pos)
    c[i]=col
    e[a[i]-1].add(col)
    e[b[i]-1].add(col)
   
print(maxi)
for color in c:
    print(color)

Buggy submission (verdict: Runtime Error):
N=int(input())
a=[0]*(N-1)
b=[0]*(N-1)
c=[0]*(N-1)
d=[[] for _ in range(N)]
e=[ set() for _ in range(N)] #存在する色のリスト
maxi=0
for i in range(N-1):
    a[i],b[i] = map(int,input().split())
    d[a[i]-1].append(b[i]-1)
    d[b[i]-1].append(a[i]-1)
for j in d:
    maxi=max(maxi,len(j))
for i in range(N-1):
    pos=set([i+1 for in range(maxi)])
    pos= pos- e[a[i]-1] -e[b[i]-1]
    col=min(pos)
    c[i]=col
    e[a[i]-1].add(col)
    e[b[i]-1].add(col)
   
print(maxi)
for color in c:
    print(color)

Buggy submission (verdict: Runtime Error):
N=int(input())
a=[0]*(N-1)
b=[0]*(N-1)
c=[0]*(N-1)
d=[[] for _ in range(N)]
e=[ set() for _ in range(N)] #存在する色のリスト
maxi=0
for i in range(N-1):
    a[i],b[i] = map(int,input().split())
    d[a[i]-1].append(b[i]-1)
    d[b[i]-1].append(a[i]-1)
for j in d:
    maxi=max(maxi,len(j))
for i in range(N-1):
    pos={1,2,3,4}
    pos= pos- e[a[i]-1] -e[b[i]-1]
    col=min(pos)
    c[i]=col
    e[a[i]-1].add(col)
    e[b[i]-1].add(col)
   
print(maxi)
for color in c:
    print(color)