Problem p02850 (python)

Fixed submission:
#AtCorder用　自動木取得コード

#１行目がノードの数、２行目以降が２つの数字でノードを繋ぐ辺を表すとき、使える

#変数
#node_n :ノードの数
#edge   :エッジを、入力の順番でリストとして提供
#【注意】ノードクラスのリストは関数で取り出さないと、連動して変わってしまう


#関数
#・getroot()で根を取り出す
#・getleaf()で葉を取り出す
#・childに子のノードクラスのリストが格納
#・parentに親のノードクラスが格納

import sys
sys.setrecursionlimit(1000000)



#ノードクラス
class Node():
    def __init__(self):
        self.name=-1
        self.parent=-1
        self.child=[]
        self.color=0
        
        
        
        
    #ノードに対する子供の数を取得
    def childnum(self):
        return len(self.child)

    

#木クラス
class Tree():
    
    
    #作成した瞬間、入力の数だけノードを作る。
    def __init__(self):
        self.tree=[]
        self.edge=[]
        self.node_n=int(input())
        for i in range(self.node_n):
            a=Node()
            a.name=i
            self.tree.append(a)
        for i in range(self.node_n-1):
            edge=[int(j) for j in input().split()]
            self.edge.append(edge)
            self.tree[edge[0]-1].child.append(self.tree[edge[1]-1])
            self.tree[edge[1]-1].parent=self.tree[edge[0]-1]
       
     
    #根ノードを取得
    def getroot(self):
        return self.tree[0]
    
    #葉ノードのリストを取得
    def getleaf(self):
        leaf=[]
        for i in self.tree:
            if i.child==[]:
                leaf.append(i)
        return leaf
    
    
    #全ノードリストを取得
    def gettree(self):
        return self.tree[:]

    
    
    
    
    
tree=Tree()

ans=0
node=tree.getroot()


def dps(Node):
    global ans
    colorlist=[]
    p_col=Node.color
    childnum=Node.childnum()
    for i in range(childnum+1):
        colorlist.append(i+1)
    if p_col in colorlist:
        colorlist.remove(p_col)
    else:
        colorlist.pop()
    
    if colorlist!=[]:
        maxcol=colorlist[-1]
        if maxcol>=ans:
            ans=maxcol
        children=Node.child
        for i in range(childnum):
            children[i].color=colorlist[i]
        for child in children:
            dps(child)
    

dps(node)

print(ans)

tre=tree.gettree()
edge=tree.edge
n=tree.node_n

for i in edge:
    print(tre[i[1]-1].color)

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(100000000)




#ノードクラス
class Node():
    def __init__(self):
        self.name=-1
        self.parent=-1
        self.p_color=0
        self.child=[]
        self.c_color=[]
        self.type=-1
    

#木クラス
class Tree():
    
    
    #作成した瞬間、入力の数だけノードを作る。
    def __init__(self):
        self.tree=[None]
        self.list=[]
        self.node_n=int(input())
        for i in range(self.node_n):
            a=Node()
            a.name=i+1
            self.tree.append(a)
        for i in range(self.node_n-1):
            hen=[int(j) for j in input().split(" ")]
            self.list.append(hen)
            self.tree[hen[0]].child.append(self.tree[hen[1]])
            self.tree[hen[1]].parent=self.tree[hen[0]]
            
        for i in range(1,self.node_n+1):
            node=self.tree[i]
            if node.parent==-1:
                node.type="root"
            elif node.child==[]:
                node.type="leaf"
            else:
                node.type=="other"
        
    def getroot(self):
        return self.tree[1]
    def getleaf(self):
        leaf=[]
        for i in self.tree:
            if i.type=="leaf":
                leaf.append(i)
        return leaf
    
    def gettree(self):
        return self.tree[:]

    
        
    
    
#木の作成
tree=Tree()


#使った色リスト
colorlist=[]


#調べたリスト
# checklist=tree.gettree()
# checklist.pop(0)
# checklist.pop(0)
# print("checklist",[i.name for i in checklist])

colvar=[tree.node_n-int(i) for i in range(tree.node_n) ]
node=tree.getroot()




def haihai(node):
#     print("node",node.name)
    parentcolor=node.p_color
    children=node.child
    if children==[]:
        pass
    else:
#         print("children",[i.name for i in children])
        childcolor=colvar.copy()
        if parentcolor in childcolor:
            childcolor.remove(parentcolor)
        for child in children:
#             print("childname",child.name)
#             checklist.remove(child)
            color=childcolor.pop()
            child.p_color=color
            if color not in colorlist :
                colorlist.append(color)
            haihai(child)
            

haihai(node)

# print(colorlist)

print(len(colorlist))


for j in [tree.tree[i[1]].p_color for i in tree.list]:
    print(j)

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(100000000)




#ノードクラス
class Node():
    def __init__(self):
        self.name=-1
        self.parent=-1
        self.p_color=0
        self.child=[]
        self.c_color=[]
        self.type=-1
    

#木クラス
class Tree():
    
    
    #作成した瞬間、入力の数だけノードを作る。
    def __init__(self):
        self.tree=[None]
        self.list=[]
        self.node_n=int(input())
        for i in range(self.node_n):
            a=Node()
            a.name=i+1
            self.tree.append(a)
        for i in range(self.node_n-1):
            hen=[int(j) for j in input().split(" ")]
            self.list.append(hen)
            self.tree[hen[0]].child.append(self.tree[hen[1]])
            self.tree[hen[1]].parent=self.tree[hen[0]]
            
        for i in range(1,self.node_n+1):
            node=self.tree[i]
            if node.parent==-1:
                node.type="root"
            elif node.child==[]:
                node.type="leaf"
            else:
                node.type=="other"
        
    def getroot(self):
        return self.tree[1]
    def getleaf(self):
        leaf=[]
        for i in self.tree:
            if i.type=="leaf":
                leaf.append(i)
        return leaf
    
    def gettree(self):
        return self.tree[:]

    
        
    
    
#木の作成
tree=Tree()


#使った色リスト
colorlist=[]


#調べたリスト
# checklist=tree.gettree()
# checklist.pop(0)
# checklist.pop(0)
# print("checklist",[i.name for i in checklist])

colvar=[tree.node_n-int(i) for i in range(tree.node_n) ]
node=tree.getroot()




def haihai(node):
#     print("node",node.name)
    parentcolor=node.p_color
    children=node.child
    if children==[]:
        pass
    else:
#         print("children",[i.name for i in children])
        childcolor=colvar.copy()
        if parentcolor in childcolor:
            childcolor.remove(parentcolor)
        for child in children:
#             print("childname",child.name)
#             checklist.remove(child)
            color=childcolor.pop()
            child.p_color=color
            if color not in colorlist :
                colorlist.append(color)
            haihai(child)
            

haihai(node)

# print(colorlist)

print(len(colorlist))


for j in [tree.tree[i[1]].p_color for i in tree.list]:
    print(j)

Buggy submission (verdict: Runtime Error):
#ノードクラス
class Node():
    def __init__(self):
        self.name=-1
        self.parent=-1
        self.p_color=0
        self.child=[]
        self.c_color=[]
        self.type=-1
    

#木クラス
class Tree():
    
    
    #作成した瞬間、入力の数だけノードを作る。
    def __init__(self):
        self.tree=[None]
        self.list=[]
        self.node_n=int(input())
        for i in range(self.node_n):
            a=Node()
            a.name=i+1
            self.tree.append(a)
        for i in range(self.node_n-1):
            hen=[int(j) for j in input().split(" ")]
            self.list.append(hen)
            self.tree[hen[0]].child.append(self.tree[hen[1]])
            self.tree[hen[1]].parent=self.tree[hen[0]]
            
        for i in range(1,self.node_n+1):
            node=self.tree[i]
            if node.parent==-1:
                node.type="root"
            elif node.child==[]:
                node.type="leaf"
            else:
                node.type=="other"
        
    def getroot(self):
        return self.tree[1]
    def getleaf(self):
        leaf=[]
        for i in self.tree:
            if i.type=="leaf":
                leaf.append(i)
        return leaf
    
    def gettree(self):
        return self.tree[:]

    
        
    
    
#木の作成
tree=Tree()


#使った色リスト
colorlist=[]


#調べたリスト
# checklist=tree.gettree()
# checklist.pop(0)
# checklist.pop(0)
# print("checklist",[i.name for i in checklist])

colvar=[tree.node_n-int(i) for i in range(tree.node_n) ]
node=tree.getroot()




def haihai(node):
#     print("node",node.name)
    parentcolor=node.p_color
    children=node.child
    if children==[]:
        pass
    else:
#         print("children",[i.name for i in children])
        childcolor=colvar.copy()
        if parentcolor in childcolor:
            childcolor.remove(parentcolor)
        for child in children:
#             print("childname",child.name)
#             checklist.remove(child)
            color=childcolor.pop()
            child.p_color=color
            if color not in colorlist :
                colorlist.append(color)
            haihai(child)
            

haihai(node)

# print(colorlist)

print(len(colorlist))


for j in [tree.tree[i[1]].p_color for i in tree.list]:
    print(j)

Buggy submission (verdict: Runtime Error):
#ノードクラス
class Node():
    def __init__(self):
        self.name=-1
        self.parent=-1
        self.p_color=0
        self.child=[]
        self.c_color=[]
        self.type=-1
    

#木クラス
class Tree():
    
    
    #作成した瞬間、入力の数だけノードを作る。
    def __init__(self):
        self.tree=[None]
        self.list=[]
        self.node_n=int(input())
        for i in range(self.node_n):
            a=Node()
            a.name=i+1
            self.tree.append(a)
        for i in range(self.node_n-1):
            hen=[int(j) for j in input().split(" ")]
            self.list.append(hen)
            self.tree[hen[0]].child.append(self.tree[hen[1]])
            self.tree[hen[1]].parent=self.tree[hen[0]]
            
        for i in range(1,self.node_n+1):
            node=self.tree[i]
            if node.parent==-1:
                node.type="root"
            elif node.child==[]:
                node.type="leaf"
            else:
                node.type=="other"
        
    def getroot(self):
        return self.tree[1]
    def getleaf(self):
        leaf=[]
        for i in self.tree:
            if i.type=="leaf":
                leaf.append(i)
        return leaf
    
    def gettree(self):
        return self.tree[:]

    
        
    
    
#木の作成
tree=Tree()


#使った色リスト
colorlist=[]


#調べたリスト
# checklist=tree.gettree()
# checklist.pop(0)
# checklist.pop(0)
# print("checklist",[i.name for i in checklist])

colvar=[tree.node_n-int(i) for i in range(tree.node_n) ]
node=tree.getroot()




def haihai(node):
#     print("node",node.name)
    parentcolor=node.p_color
    children=node.child
    if children==[]:
        pass
    else:
#         print("children",[i.name for i in children])
        childcolor=colvar.copy()
        if parentcolor in childcolor:
            childcolor.remove(parentcolor)
        for child in children:
#             print("childname",child.name)
#             checklist.remove(child)
            color=childcolor.pop()
            child.p_color=color
            if color not in colorlist :
                colorlist.append(color)
            haihai(child)
            

haihai(node)

# print(colorlist)

print(len(colorlist))


for j in [tree.tree[i[1]].p_color for i in tree.list]:
    print(j)

Buggy submission (verdict: Runtime Error):
#ノードクラス
class Node():
    def __init__(self):
        self.name=-1
        self.parent=-1
        self.p_color=0
        self.child=[]
        self.c_color=[]
        self.type=-1
    

#木クラス
class Tree():
    
    
    #作成した瞬間、入力の数だけノードを作る。
    def __init__(self):
        self.tree=[None]
        self.list=[]
        self.node_n=int(input())
        for i in range(self.node_n):
            a=Node()
            a.name=i+1
            self.tree.append(a)
        for i in range(self.node_n-1):
            hen=[int(j) for j in input().split(" ")]
            self.list.append(hen)
            self.tree[hen[0]].child.append(self.tree[hen[1]])
            self.tree[hen[1]].parent=self.tree[hen[0]]
            
        for i in range(1,self.node_n+1):
            node=self.tree[i]
            if node.parent==-1:
                node.type="root"
            elif node.child==[]:
                node.type="leaf"
            else:
                node.type=="other"
        
    def getroot(self):
        return self.tree[1]
    def getleaf(self):
        leaf=[]
        for i in self.tree:
            if i.type=="leaf":
                leaf.append(i)
        return leaf
    
    def gettree(self):
        return self.tree[:]

    
        
    
    
#木の作成
tree=Tree()


#使った色リスト
colorlist=[]


#調べたリスト
# checklist=tree.gettree()
# checklist.pop(0)
# checklist.pop(0)
# print("checklist",[i.name for i in checklist])

colvar=[tree.node_n-int(i) for i in range(tree.node_n) ]
node=tree.getroot()




def haihai(node):
    print("node",node.name)
    parentcolor=node.p_color
    children=node.child
    if children==[]:
        pass
    else:
        print("children",[i.name for i in children])
        childcolor=colvar.copy()
        if parentcolor in childcolor:
            childcolor.remove(parentcolor)
        for child in children:
            print("childname",child.name)
#             checklist.remove(child)
            color=childcolor.pop()
            child.p_color=color
            if color not in colorlist :
                colorlist.append(color)
            haihai(child)
            

haihai(node)

print(colorlist)

print(len(colorlist))


for j in [tree.tree[i[1]].p_color for i in tree.list]:
    print(j)

Buggy submission (verdict: Runtime Error):
#ノードクラス
class Node():
    def __init__(self):
        self.name=-1
        self.parent=-1
        self.p_color=0
        self.child=[]
        self.c_color=[]
        self.type=-1
    

#木クラス
class Tree():
    
    
    #作成した瞬間、入力の数だけノードを作る。
    def __init__(self):
        self.tree=[None]
        self.list=[]
        self.node_n=int(input())
        for i in range(self.node_n):
            a=Node()
            a.name=i+1
            self.tree.append(a)
        for i in range(self.node_n-1):
            hen=[int(j) for j in input().split(" ")]
            self.list.append(hen)
            self.tree[hen[0]].child.append(self.tree[hen[1]])
            self.tree[hen[1]].parent=self.tree[hen[0]]
            
        for i in range(1,self.node_n+1):
            node=self.tree[i]
            if node.parent==-1:
                node.type="root"
            elif node.child==[]:
                node.type="leaf"
            else:
                node.type=="other"
        
    def getroot(self):
        return self.tree[1]
    def getleaf(self):
        leaf=[]
        for i in self.tree:
            if i.type=="leaf":
                leaf.append(i)
        return leaf
    
    def gettree(self):
        return self.tree[:]

    
        
    
    
#木の作成
tree=Tree()


#使った色リスト
colorlist=[]


#調べたリスト
# checklist=tree.gettree()
# checklist.pop(0)
# checklist.pop(0)
# print("checklist",[i.name for i in checklist])

colvar=[tree.node_n-int(i) for i in range(tree.node_n) ]
node=tree.getroot()




def haihai(node):
    print("node",node.name)
    parentcolor=node.p_color
    children=node.child
    if children==[]:
        pass
    else:
        print("children",[i.name for i in children])
        childcolor=colvar.copy()
        if parentcolor in childcolor:
            childcolor.remove(parentcolor)
        for child in children:
            print("childname",child.name)
#             checklist.remove(child)
            color=childcolor.pop()
            child.p_color=color
            if color not in colorlist :
                colorlist.append(color)
            haihai(child)
            

haihai(node)

print(colorlist)

print(len(colorlist))


for j in [tree.tree[i[1]].p_color for i in tree.list]:
    print(j)

Buggy submission (verdict: Runtime Error):
#ノードクラス
class Node():
    def __init__(self):
        self.name=-1
        self.parent=-1
        self.p_color=0
        self.child=[]
        self.c_color=[]
        self.type=-1
    

#木クラス
class Tree():
    
    
    #作成した瞬間、入力の数だけノードを作る。
    def __init__(self):
        self.tree=[None]
        self.list=[]
        self.node_n=int(input())
        for i in range(self.node_n):
            a=Node()
            a.name=i+1
            self.tree.append(a)
        for i in range(self.node_n-1):
            hen=[int(j) for j in input().split(" ")]
            self.list.append(hen)
            self.tree[hen[0]].child.append(self.tree[hen[1]])
            self.tree[hen[1]].parent=self.tree[hen[0]]
            
        for i in range(1,self.node_n+1):
            node=self.tree[i]
            if node.parent==-1:
                node.type="root"
            elif node.child==[]:
                node.type="leaf"
            else:
                node.type=="other"
        
    def getroot(self):
        return self.tree[1]
    def getleaf(self):
        leaf=[]
        for i in self.tree:
            if i.type=="leaf":
                leaf.append(i)
        return leaf
    
    def gettree(self):
        return self.tree

    
        
    
    
#木リストにノードを入れる
tree=Tree()


#使った色リスト
colorlist=[]


#調べたリスト
checklist=tree.gettree()
checklist=checklist[:]
checklist.pop(0)
checklist.pop(0)
# print("checklist",[i.name for i in checklist])

colvar=[len(checklist)-int(i) for i in range(len(checklist)) ]
node=tree.getroot()




def haihai(node):
#     print("node",node.name)
    parentcolor=node.p_color
    children=node.child
    if children==[]:
        pass
    else:
#         print("children",[i.name for i in children])
        childcolor=colvar[:]
        if parentcolor in childcolor:
            childcolor.remove(parentcolor)
        for child in children:
#             print("childname",child.name)
            checklist.remove(child)
            color=childcolor.pop()
            child.p_color=color
            if color not in colorlist :
                colorlist.append(color)
            haihai(child)
            

# print(colorlist)
haihai(node)

# print(colorlist)

print(len(colorlist))


for j in [tree.tree[i[1]].p_color for i in tree.list]:
    print(j)