Problem p02850 (python)

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline

class Node:
    nodes = {}
    max_c = 0
    @classmethod
    def get_root_node(cls):
        n = Node.nodes[1]
        for _ in range(1000000):
            if n.parent is None:
                return n
            else:
                n = n.parent

    @classmethod
    def dump_all_nodes(cls):
        for key, node in Node.nodes.items():
            print(str(node))

    @classmethod
    def get_instance(cls, id, parent):
        if id in Node.nodes:
            if Node.nodes[id].parent is None:
                Node.nodes[id].parent = parent
            return Node.nodes[id]
        else:
            return Node(id, parent)

    @classmethod
    def color_childs(clf, start_node):
        c = 1
        list_to_visit = deque(start_node.childs)
        list_to_visit.append(-1)
        ap = list_to_visit.append

        while(len(list_to_visit)>0):
            if list_to_visit[0]==-1:
                c = 1
                list_to_visit.popleft()
                continue
            node = list_to_visit[0]
            list_to_visit.popleft()
            if c == node.parent.parent_edge_color:
                c += 1
            node.parent_edge_color = c
            if c > Node.max_c:
                Node.max_c = c
            list_to_visit += deque(node.childs)
            ap(-1)
            c += 1

    def __init__(self, id, parent):
        self.id = id
        self.parent = parent # None if root Node
        self.parent_edge_color = None
        self.childs = []
        Node.nodes[id] = self
        self.used_color = []

    def __str__(self):
        x = self.parent.id if self.parent else "None"
        #return f"id: {self.id}, parent: {x}, parent_edge_color: {self.parent_edge_color} ,num_child {len(self.childs)}, num_color {self.num_color}"
        return str(self.id)

    def append_child(self, node):
        self.childs.append(node)

    @property
    def num_color(self):
        if self.parent_edge_color is None:
            return len(self.childs)
        else:
            return len(self.childs) + 1

N = int(input())

Edges = []
Edges_color = []
ap = Edges.append
for i in range(N-1):
    edge = list(map(int, input().split()))
    ap(edge)
    pnode = Node.get_instance(edge[0], None)
    cnode = Node.get_instance(edge[1], pnode)
    pnode.append_child(cnode)

root = Node.get_root_node()

Node.color_childs(root)
#Node.dump_all_nodes()
K = Node.max_c
print(K)

for edge in Edges:
    print(Node.get_instance(edge[1], edge[0]).parent_edge_color)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

class Node:
    nodes = {}
    @classmethod
    def get_root_node(cls):
        #n = Node.nodes[next(iter(Node.nodes))]
        n = Node.nodes[1]
        for _ in range(len(Node.nodes)):
            if n.parent is None:
                return n
            else:
                n = n.parent

    @classmethod
    def largest_num_color(cls):
        r = 0
        for key, node in Node.nodes.items():
            if node.num_color > r:
                r = node.num_color
        return r

    @classmethod
    def dump_all_nodes(cls):
        for key, node in Node.nodes.items():
            print(str(node))

    @classmethod
    def get_instance(cls, id, parent):
        if id in Node.nodes:
            if Node.nodes[id].parent is None:
                Node.nodes[id].parent = parent
            return Node.nodes[id]
        else:
            return Node(id, parent)

    @classmethod
    def color_childs(clf, start_node):
        c = 1
        list_to_visit = []
        list_to_visit += start_node.childs
        list_to_visit.append(-1)


        #while(len(list_to_visit)>0):
        for i in range(1000000)
            if len(list_to_visit)==0:
                break
            if list_to_visit[0]==-1:
                c = 1
                list_to_visit.remove(list_to_visit[0])
                continue
            node = list_to_visit[0]
            list_to_visit.remove(list_to_visit[0])
            if c == node.parent.parent_edge_color:
                c += 1
            node.parent_edge_color = c
            list_to_visit += node.childs
            list_to_visit.append(-1)
            c += 1

    def __init__(self, id, parent):
        self.id = id
        self.parent = parent # None if root Node
        self.parent_edge_color = None
        self.childs = []
        Node.nodes[id] = self
        self.used_color = []

    def __str__(self):
        x = self.parent.id if self.parent else "None"
        #return f"id: {self.id}, parent: {x}, parent_edge_color: {self.parent_edge_color} ,num_child {len(self.childs)}, num_color {self.num_color}"
        return str(self.id)

    def append_child(self, node):
        self.childs.append(node)

    @property
    def num_color(self):
        if self.parent_edge_color is None:
            return len(self.childs)
        else:
            return len(self.childs) + 1

N = int(input())

Edges = []
Edges_color = []
for i in range(N-1):
    edge = list(map(int, input().split()))
    Edges.append(edge)

for edge in Edges:
    pnode = Node.get_instance(edge[0], None)
    cnode = Node.get_instance(edge[1], pnode)
    pnode.append_child(cnode)

root = Node.get_root_node()

Node.color_childs(root)
#Node.dump_all_nodes()
K = Node.largest_num_color()
print(K)

for edge in Edges:
    print(Node.get_instance(edge[1], edge[0]).parent_edge_color)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(1000000)
class Node:
    nodes = {}
    @classmethod
    def get_root_node(cls):
        #n = Node.nodes[next(iter(Node.nodes))]
        n = Node.nodes[1]
        for _ in range(len(Node.nodes)):
            if n.parent is None:
                return n
            else:
                n = n.parent

    @classmethod
    def largest_num_color(cls):
        r = 0
        for key, node in Node.nodes.items():
            if node.num_color > r:
                r = node.num_color
        return r

    @classmethod
    def dump_all_nodes(cls):
        for key, node in Node.nodes.items():
            print(str(node))

    @classmethod
    def get_instance(cls, id, parent):
        if id in Node.nodes:
            if Node.nodes[id].parent is None:
                Node.nodes[id].parent = parent
            return Node.nodes[id]
        else:
            return Node(id, parent)

    @classmethod
    def color_childs(clf, start_node):
        c = 1
        list_to_visit = []
        list_to_visit += start_node.childs
        list_to_visit.append(-1)
        while(len(list_to_visit)>0):
            if list_to_visit[0]==-1:
                c = 1
                list_to_visit.remove(list_to_visit[0])
                continue
            node = list_to_visit[0]
            list_to_visit.remove(list_to_visit[0])
            if c == node.parent.parent_edge_color:
                c += 1
            node.parent_edge_color = c
            list_to_visit += node.childs
            list_to_visit.append(-1)
            c += 1

    def __init__(self, id, parent):
        self.id = id
        self.parent = parent # None if root Node
        self.parent_edge_color = None
        self.childs = []
        Node.nodes[id] = self
        self.used_color = []

    def __str__(self):
        x = self.parent.id if self.parent else "None"
        return f"id: {self.id}, parent: {x}, parent_edge_color: {self.parent_edge_color} ,num_child {len(self.childs)}, num_color {self.num_color}"

    def append_child(self, node):
        self.childs.append(node)

    @property
    def num_color(self):
        if self.parent_edge_color is None:
            return len(self.childs)
        else:
            return len(self.childs) + 1

N = int(input())

Edges = []
Edges_color = []
for i in range(N-1):
    edge = list(map(int, input().split()))
    Edges.append(edge)


for edge in Edges:
    pnode = Node.get_instance(edge[0], None)
    cnode = Node.get_instance(edge[1], pnode)
    pnode.append_child(cnode)

root = Node.get_root_node()

Node.color_childs(root)
#Node.dump_all_nodes()
K = Node.largest_num_color()
print(K)

for edge in Edges:
    print(Node.get_instance(edge[1], edge[0]).parent_edge_color)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(1000000) 
class Node:
    nodes = {}
    @classmethod
    def get_root_node(cls):
        n = Node.nodes[next(iter(Node.nodes))]
        while True:
            if n.parent is None:
                return n
            else:
                n = n.parent

    @classmethod
    def largest_num_color(cls):
        r = 0
        for key, node in Node.nodes.items():
            if node.num_color > r:
                r = node.num_color
        return r

    @classmethod
    def dump_all_nodes(cls):
        for key, node in Node.nodes.items():
            print(str(node))

    @classmethod
    def get_instance(cls, id, parent):
        if id in Node.nodes:
            if Node.nodes[id].parent is None:
                Node.nodes[id].parent = parent
            return Node.nodes[id]
        else:
            return Node(id, parent)

    @classmethod
    def color_childs(clf, start_node):
        c = 1
        list_to_visit = []
        list_to_visit += start_node.childs
        list_to_visit.append(-1)
        while(len(list_to_visit)>0):
            if list_to_visit[0]==-1:
                c = 1
                list_to_visit.remove(list_to_visit[0])
                continue
            node = list_to_visit[0]
            list_to_visit.remove(list_to_visit[0])
            if c == node.parent.parent_edge_color:
                c += 1
            node.parent_edge_color = c
            list_to_visit += node.childs
            list_to_visit.append(-1)
            c += 1

    def __init__(self, id, parent):
        self.id = id
        self.parent = parent # None if root Node
        self.parent_edge_color = None
        self.childs = []
        Node.nodes[id] = self
        self.used_color = []

    def __str__(self):
        x = self.parent.id if self.parent else "None"
        return f"id: {self.id}, parent: {x}, parent_edge_color: {self.parent_edge_color} ,num_child {len(self.childs)}, num_color {self.num_color}"

    def append_child(self, node):
        self.childs.append(node)

    @property
    def num_color(self):
        if self.parent_edge_color is None:
            return len(self.childs)
        else:
            return len(self.childs) + 1

N = int(input())

Edges = []
Edges_color = []
for i in range(N-1):
    edge = list(map(int, input().split()))
    Edges.append(edge)


for edge in Edges:
    pnode = Node.get_instance(edge[0], None)
    cnode = Node.get_instance(edge[1], pnode)
    pnode.append_child(cnode)

root = Node.get_root_node()
Node.color_childs(root)
#Node.dump_all_nodes()
K = Node.largest_num_color()
print(K)

for edge in Edges:
    print(Node.get_instance(edge[1], edge[0]).parent_edge_color)

Buggy submission (verdict: Runtime Error):

class Node(object):
    nodes = {}
    @classmethod
    def get_root_node(cls):
        n = Node.nodes[next(iter(Node.nodes))]
        while True:
            if n.parent is None:
                return n
            else:
                n = n.parent

    @classmethod
    def largest_num_color(cls):
        r = 0
        for key, node in Node.nodes.items():
            if node.num_color > r:
                r = node.num_color
        return r

    @classmethod
    def dump_all_nodes(cls):
        for key, node in Node.nodes.items():
            print(str(node))

    @classmethod
    def get_instance(cls, id, parent):
        if id in Node.nodes:
            if Node.nodes[id].parent is None:
                Node.nodes[id].parent = parent
            return Node.nodes[id]
        else:
            return Node(id, parent)

    @classmethod
    def color_childs(clf, start_node):
        c = 1
        list_to_visit = []
        list_to_visit += start_node.childs
        list_to_visit.append(-1)
        while(len(list_to_visit)>0):
            if list_to_visit[0]==-1:
                c = 1
                list_to_visit.remove(list_to_visit[0])
                continue
            node = list_to_visit[0]
            list_to_visit.remove(list_to_visit[0])
            if c == node.parent.parent_edge_color:
                c += 1
            node.parent_edge_color = c
            list_to_visit += node.childs
            list_to_visit.append(-1)
            c += 1

    def __init__(self, id, parent):
        self.id = id
        self.parent = parent # None if root Node
        self.parent_edge_color = None
        self.childs = []
        Node.nodes[id] = self
        self.used_color = []

    def __str__(self):
        x = self.parent.id if self.parent else "None"
        return f"id: {self.id}, parent: {x}, parent_edge_color: {self.parent_edge_color} ,num_child {len(self.childs)}, num_color {self.num_color}"

    def append_child(self, node):
        self.childs.append(node)

    @property
    def num_color(self):
        if self.parent_edge_color is None:
            return len(self.childs)
        else:
            return len(self.childs) + 1

N = int(input())

Edges = []
Edges_color = []
for i in range(N-1):
    edge = list(map(int, input().split()))
    Edges.append(edge)


for edge in Edges:
    pnode = Node.get_instance(edge[0], None)
    cnode = Node.get_instance(edge[1], pnode)
    pnode.append_child(cnode)

root = Node.get_root_node()
Node.color_childs(root)
#Node.dump_all_nodes()
K = Node.largest_num_color()
print(K)

for edge in Edges:
    print(Node.get_instance(edge[1], edge[0]).parent_edge_color)

Buggy submission (verdict: Runtime Error):

class Node:
    nodes = {}
    @classmethod
    def get_root_node(cls):
        n = Node.nodes[next(iter(Node.nodes))]
        while True:
            if n.parent is None:
                return n
            else:
                n = n.parent

    @classmethod
    def largest_num_color(cls):
        r = 0
        for key, node in Node.nodes.items():
            if node.num_color > r:
                r = node.num_color
        return r

    @classmethod
    def dump_all_nodes(cls):
        for key, node in Node.nodes.items():
            print(str(node))

    @classmethod
    def get_instance(cls, id, parent):
        if id in Node.nodes:
            if Node.nodes[id].parent is None:
                Node.nodes[id].parent = parent
            return Node.nodes[id]
        else:
            return Node(id, parent)

    def __init__(self, id, parent):
        self.id = id
        self.parent = parent # None if root Node
        self.parent_edge_color = None
        self.childs = []
        Node.nodes[id] = self
        self.used_color = []

    def __str__(self):
        x = self.parent.id if self.parent else "None"
        return f"id: {self.id}, parent: {x}, parent_edge_color: {self.parent_edge_color} ,num_child {len(self.childs)}, num_color {self.num_color}"

    def append_child(self, node):
        self.childs.append(node)

    def set_parent_edge_color(self, c):
        self.parent_edge_color = c

    @classmethod
    def color_childs(clf, start_node):
        c = 1
        list_to_visit = []
        list_to_visit += start_node.childs
        list_to_visit.append(-1)
        while(len(list_to_visit)>0):
            #print("-----")
            #print("length"+ str(len(list_to_visit)))
            #print("visit to: " + str(list_to_visit[0]))
            #print("color with: "+ str(c))

            if list_to_visit[0]==-1:
            #    print("CAME HERE")
                c = 1
            #    print(c)
                list_to_visit.remove(list_to_visit[0])
                continue
            node = list_to_visit[0]
            list_to_visit.remove(list_to_visit[0])
            if c == node.parent.parent_edge_color:
                c += 1
            node.parent_edge_color = c
            #print("after color")
            #print(node)
            list_to_visit += node.childs
            list_to_visit.append(-1)
            c += 1
            #print("-----")

    @property
    def num_color(self):
        if self.parent_edge_color is None:
            return len(self.childs)
        else:
            return len(self.childs) + 1

N = int(input())

Edges = []
Edges_color = []
for i in range(N-1):
    edge = list(map(int, input().split()))
    Edges.append(edge)


for edge in Edges:
    pnode = Node.get_instance(edge[0], None)
    cnode = Node.get_instance(edge[1], pnode)
    pnode.append_child(cnode)

root = Node.get_root_node()
Node.color_childs(root)
#Node.dump_all_nodes()

# ----- output -----

#print("output:=========================")

K = Node.largest_num_color()
print(K)

for edge in Edges:
    print(Node.get_instance(edge[1], edge[0]).parent_edge_color)

Buggy submission (verdict: Runtime Error):
class Node:
    nodes = {}
    @classmethod
    def get_root_node(cls):
        n = Node.nodes[next(iter(Node.nodes))]
        while True:
            if n.parent is None:
                return n
            else:
                n = n.parent

    @classmethod
    def largest_num_color(cls):
        r = 0
        for key, node in Node.nodes.items():
            if node.num_color > r:
                r = node.num_color
        return r

    @classmethod
    def dump_all_nodes(cls):
        for key, node in Node.nodes.items():
            print(str(node))

    @classmethod
    def get_instance(cls, id, parent):
        if id in Node.nodes:
            if Node.nodes[id].parent is None:
                Node.nodes[id].parent = parent
            return Node.nodes[id]
        else:
            return Node(id, parent)

    def __init__(self, id, parent):
        self.id = id
        self.parent = parent # None if root Node
        self.parent_edge_color = None
        self.childs = {}
        Node.nodes[id] = self
        self.used_color = []

    def __str__(self):
        x = self.parent.id if self.parent else "None"
        return f"id: {self.id}, parent: {x}, parent_edge_color: {self.parent_edge_color} ,num_child {len(self.childs)}, num_color {self.num_color}"

    def append_child(self, node):
        self.childs[node.id] = node

    def set_parent_edge_color(self, c):
        self.parent_edge_color = c

    def color_childs(self):
        c = 1
        for key, child_node in self.childs.items():
            if c == self.parent_edge_color:
                c += 1
            child_node.parent_edge_color = c
            c += 1
            child_node.color_childs()

    @property
    def num_color(self):
        if self.parent_edge_color is None:
            return len(self.childs)
        else:
            return len(self.childs) + 1

N = int(input())

Edges = []
Edges_color = []
for i in range(N-1):
    edge = list(map(int, input().split()))
    Edges.append(edge)


for edge in Edges:
    pnode = Node.get_instance(edge[0], None)
    cnode = Node.get_instance(edge[1], pnode)
    pnode.append_child(cnode)

root = Node.get_root_node()
root.color_childs()
#Node.dump_all_nodes()

# ----- output -----

K = Node.largest_num_color()
print(K)

for edge in Edges:
    print(Node.get_instance(edge[1], edge[0]).parent_edge_color)

Buggy submission (verdict: Runtime Error):
class Node:
    nodes = {}

    @classmethod
    def get_root_node(cls):
        n = Node.nodes[next(iter(Node.nodes))]
        while True:
            if n.parent is None:
                return n
            else:
                n = n.parent

    @classmethod
    def largest_child(cls):
        r = 0
        for key, node in Node.nodes.items():
            if len(node.childs) > r:
                r = len(node.childs)
        return r

    @classmethod
    def dump_all_nodes(cls):
        for key, node in Node.nodes.items():
            print(str(node))

    @classmethod
    def get_instance(cls, id, parent):
        if id in Node.nodes:
            if Node.nodes[id].parent is None:
                Node.nodes[id].parent = parent
            return Node.nodes[id]
        else:
            return Node(id, parent)

    def __init__(self, id, parent):
        self.id = id
        self.parent = parent # None if root Node
        self.parent_edge_color = None
        self.childs = {}
        Node.nodes[id] = self
        self.used_color = []

    def __str__(self):
        x = self.parent.id if self.parent else "None"
        return f"id: {self.id}, parent: {x}, parent_edge_color: {self.parent_edge_color} ,num_child {len(self.childs)}"

    def append_child(self, node):
        self.childs[node.id] = node

    def set_parent_edge_color(self, c):
        print(c)
        self.parent_edge_color = c

    def color_childs(self):
        c = 0
        for key, child_node in self.childs.items():
            if c == self.parent_edge_color:
                c += 1
            child_node.parent_edge_color = c
            c += 1
            child_node.color_childs()

N = int(input())

Edges = []
Edges_color = []
for i in range(N-1):
    edge = list(map(int, input().split()))
    Edges.append(edge)


for edge in Edges:
    pnode = Node.get_instance(edge[0], None)
    cnode = Node.get_instance(edge[1], pnode)
    pnode.append_child(cnode)

K = Node.largest_child() + 1
print(K)

root = Node.get_root_node()

root.color_childs()
#Node.dump_all_nodes() #ok

for edge in Edges:
    print(Node.get_instance(edge[1], edge[0]).parent_edge_color)