Problem p02850 (python)

Fixed submission:
from sys import setrecursionlimit


def genid(a, b):
    if b < a:
        a, b = b, a
    return a * 100000 + b


def paint(currentNode, usedColor, parentNode, edges, colors):
    color = 1
    for childNode in edges[currentNode]:
        if childNode == parentNode:
            continue
        if color == usedColor:
            color += 1
        colors[genid(currentNode, childNode)] = color
        paint(childNode, color, currentNode, edges, colors)
        color += 1


setrecursionlimit(100000)

N = int(input())
ab = [list(map(int, input().split())) for _ in range(N - 1)]

edges = [[] for _ in range(N)]
for a, b in ab:
    edges[a - 1].append(b - 1)
    edges[b - 1].append(a - 1)

colors = {}
paint(0, -1, -1, edges, colors)

print(max(len(e) for e in edges))
for a, b in ab:
    print(colors[genid(a - 1, b - 1)])

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = []
for i in range(n - 1):
    arr.append([int(s) for s in input().split(" ")])

mmk = {}

childNodeNums = [0 for i in range(n)]
childNodeStatus = [-1 for i in range(n)]


def generateID(fr, to):
    if to < fr:
        fr, to = to, fr
    return fr * 100000 + to


def prepare(n, arr):
    s = [[] for i in range(n)]
    for ar in arr:
        s[ar[0] - 1].append(ar[1] - 1)
        s[ar[1] - 1].append(ar[0] - 1)
    return s


# x为记录入边的颜色
def dfs(currentNode, arr, x):
    childNodes = arr[currentNode]
    if len(childNodes) == 0:
        return

    childNodeNums[currentNode] = childNodeNums[currentNode] + len(arr[currentNode])
    childNodeStatus[currentNode] = 1

    kk = 0
    for i, childNodeIndex in enumerate(childNodes):

        if childNodeStatus[childNodeIndex] == -1:
            kk = kk + 1
            if x == kk:
                kk = kk + 1
            mmk[generateID(currentNode + 1,childNodeIndex + 1)] = kk
            dfs(childNodeIndex, arr, kk)


data = prepare(n, arr)
dfs(0, data, -1)

print(max(childNodeNums))

for ar in arr:
    a0 = ar[0]
    a1 = ar[1]
    print(mmk[generateID(a0,a1)])

Buggy submission (verdict: Runtime Error):
from collections import deque


n = int(input())
arr = []
for i in range(n - 1):
    arr.append([int(s) for s in input().split(" ")])

status = [-1 for i in range(n)]
status2 = [-1 for i in range(n)]
childNodeNums = [0 for i in range(n)]


def prepare(n, arr):
    data = [[] for i in range(n)]
    lens = [0 for i in range(n)]
    for ar in arr:
        data[ar[0] - 1].append(ar[1] - 1)
        lens[ar[0] - 1] += 1
        data[ar[1] - 1].append(ar[0] - 1)
        lens[ar[1] - 1] += 1
    return data, lens


def dfs(currentNode, arr):
    status[currentNode] = 1
    childNodes = arr[currentNode]

    for j in childNodes:
        if status[j] == -1:
            childNodeNums[currentNode] += 1
            dfs(j, arr)

class Node:
    def __init__(self,index,parentNum):
        self.index = index
        self.parentNum = parentNum


def bfs(arr):
    while len(q) > 0:
        node = q.popleft()
        status2[node.index] = 1
        if node is not None:
            childNodes = arr[node.index]
            kk = 0
            for index, val in enumerate(childNodes):
                if status2[val] == -1:
                    kk = kk + 1
                    if kk == node.parentNum:
                        kk = kk + 1
                    print(kk)
                    q.append(Node(val,kk))


data, lens = prepare(n, arr)
dfs(0, data)
rootIndex = childNodeNums.index(max(childNodeNums))
print(max(lens))

q = deque()
q.append(Node(rootIndex,-1))
bfs(data)

Buggy submission (verdict: Runtime Error):
import numpy as np
n = int(input())
arr = []
for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

mmk = np.zeros([n+1,n+1],dtype=int)

childNodeNums = [0 for i in range(n)]
childNodeStatus = [-1 for i in range(n)]
nodeLines = [0 for i in range(n - 1)]


def prepare(n, arr):
    s = [[] for i in range(n)]
    for ar in arr:
        s[ar[0] - 1].append(ar[1] - 1)
        s[ar[1] - 1].append(ar[0] - 1)
    return s


# x为记录入边的颜色
def dfs(currentNode, arr, x):
    childNodes = arr[currentNode]
    if len(childNodes) == 0:
        return

    childNodeNums[currentNode] = childNodeNums[currentNode] + len(arr[currentNode])
    childNodeStatus[currentNode] = 1

    kk = 0
    for i, childNodeIndex in enumerate(childNodes):

        if childNodeStatus[childNodeIndex] == -1:
            kk = kk + 1
            if x == kk:
                kk = kk + 1
            mmk[currentNode+1][childNodeIndex+1] = kk
            dfs(childNodeIndex, arr, kk)

data = prepare(n, arr)
dfs(0, data, -1)

print(max(childNodeNums))

for ar in arr:
    a0 = ar[0]
    a1 = ar[1]
    print(mmk[a0][a1])

Buggy submission (verdict: Runtime Error):
import numpy as np
n = int(input())
arr = []
for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

mmk = np.zeros([n+1,n+1],dtype=int)

childNodeNums = np.zeros(n,dtype=int)
childNodeStatus = [-1 for i in range(n)]


def prepare(n, arr):
    s = [[] for i in range(n)]
    for ar in arr:
        s[ar[0] - 1].append(ar[1] - 1)
        s[ar[1] - 1].append(ar[0] - 1)
    return s


# x为记录入边的颜色
def dfs(currentNode, arr, x):
    childNodes = arr[currentNode]
    if len(childNodes) == 0:
        return

    childNodeNums[currentNode] = childNodeNums[currentNode] + len(arr[currentNode])
    childNodeStatus[currentNode] = 1

    kk = 0
    for i, childNodeIndex in enumerate(childNodes):

        if childNodeStatus[childNodeIndex] == -1:
            kk = kk + 1
            if x == kk:
                kk = kk + 1
            mmk[currentNode+1][childNodeIndex+1] = kk
            dfs(childNodeIndex, arr, kk)




data = prepare(n, arr)
dfs(0, data, -1)

print(max(childNodeNums))

for ar in arr:
    a0 = ar[0]
    a1 = ar[1]
    print(mmk[a0][a1])

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = []
for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])


status = [-1 for i in range(n)]
status2 = [-1 for i in range(n)]
childNodeNums = [0 for i in range(n)]


def prepare(n, arr):
    data = [[] for i in range(n)]
    lens =  [0 for i in range(n)]
    for ar in arr:
        data[ar[0] - 1].append(ar[1] - 1)
        lens[ar[0]-1] += 1
        data[ar[1] - 1].append(ar[0] - 1)
        lens[ar[1]-1] += 1
    return data,lens


def dfs(currentNode, arr):
    status[currentNode] = 1
    childNodes = arr[i]

    for j in childNodes:
        if status[j] == -1:
            childNodeNums[currentNode] += 1
            dfs(j, arr)


def dfs2(currentNode, arr, x):
    status2[currentNode] = 1
    childNodes = arr[currentNode]
    kk = 0
    for index,val in enumerate(childNodes):
        if status2[val] == -1:
            kk = kk + 1
            if kk == x:
                kk = kk + 1
            print(kk)

    kk2 = 0
    for index,val in enumerate(childNodes):
        if status2[val] == -1:
            kk2 = kk2 + 1
            if kk2 == x:
                kk2 = kk2 + 1
            dfs2(val,arr,kk2)


data,lens = prepare(n, arr)
dfs(0, data)
lastIndex = childNodeNums.index(max(childNodeNums))
print(max(lens))
dfs2(lastIndex,data,-1)

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = []
for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])


status = [-1 for i in range(n)]
status2 = [-1 for i in range(n)]
childNodeNums = [0 for i in range(n)]


def prepare(n, arr):
    data = [[] for i in range(n)]

    for ar in arr:
        data[ar[0] - 1].append(ar[1] - 1)
        data[ar[1] - 1].append(ar[0] - 1)

    return data


def dfs(currentNode, arr):
    status[currentNode] = 1
    for i in range(n):
        childNodes = arr[i]

        for j in childNodes:
            if status[j] == -1:
                childNodeNums[i] += 1
                dfs(j, arr)


def dfs2(currentNode, arr, x):
    status2[currentNode] = 1
    childNodes = arr[currentNode]
    kk = 0
    for index,val in enumerate(childNodes):
        if status2[val] == -1:
            kk = kk + 1
            if kk == x:
                kk = kk + 1
            print(kk)

    kk2 = 0
    for index,val in enumerate(childNodes):
        if status2[val] == -1:
            kk2 = kk2 + 1
            if kk2 == x:
                kk2 = kk2 + 1
            dfs2(val,arr,kk2)


data = prepare(n, arr)
dfs(0, data)
lastIndex = childNodeNums.index(max(childNodeNums))
dfs2(lastIndex,data,-1)

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = []
for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

childNodeNums = [0 for i in range(n)]
childNodeStatus = [-1 for i in range(n)]
nodeLines = [[] for i in range(n - 1)]


def prepare(n, arr):
    s = [[] for i in range(n)]
    for ar in arr:
        s[ar[0] - 1].append(ar[1] - 1)
        s[ar[1] - 1].append(ar[0] - 1)
    return s


# x为记录入边的颜色
def dfs(currentNode, arr, x):
    childNodes = arr[currentNode]
    if len(childNodes) == 0:
        return

    childNodeNums[currentNode] = childNodeNums[currentNode] + len(arr[currentNode])
    childNodeStatus[currentNode] = 1

    kk = 0
    for i, childNodeIndex in enumerate(childNodes):

        if childNodeStatus[childNodeIndex] == -1:
            kk = kk + 1
            if x == kk:
                kk = kk + 1
            nodeLines[currentNode].append(kk)
            dfs(childNodeIndex, arr, kk)



data = prepare(n, arr)
dfs(0, data, -1)

print(max(childNodeNums))

for ar in nodeLines:
    for r in ar:
        print(r)

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = []
for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

# n = 9
# arr = [
#     [1,2],
#     [1,6],
#     [2,8],
#     [2,3],
#     [2,7],
#     [3,9],
#     [3,4],
#     [4,5],
# ]

childNodeNums = [0 for i in range(n)]
childNodeStatus = [-1 for i in range(n)]
nodeLines = [[] for i in range(n - 1)]


def prepare(n, arr):
    s = [[] for i in range(n)]
    for ar in arr:
        s[ar[0] - 1].append(ar[1] - 1)
        s[ar[1] - 1].append(ar[0] - 1)
    return s


# x为记录入边的颜色
def dfs(currentNode, arr, x):
    m1 = childNodeNums
    m2 = childNodeStatus
    childNodes = arr[currentNode]
    if len(childNodes) == 0:
        return

    childNodeNums[currentNode] = childNodeNums[currentNode] + len(arr[currentNode])
    childNodeStatus[currentNode] = 1

    kk = 0
    for i, childNodeIndex in enumerate(childNodes):

        if childNodeStatus[childNodeIndex] == -1:
            kk = kk + 1
            if x == kk:
                kk = kk + 1
            nodeLines[currentNode].append(kk)
            dfs(childNodeIndex, arr, kk)



data = prepare(n, arr)
dfs(0, data, -1)

print(max(childNodeNums))

for ar in nodeLines:
    for a in ar:
        print(a)

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = []
for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

childNodeNums = [0 for i in range(n)]
childNodeStatus = [-1 for i in range(n)]
nodeLines = [[] for i in range(n - 1)]


def prepare(n, arr):
    s = [[] for i in range(n)]
    for ar in arr:
        s[ar[0] - 1].append(ar[1] - 1)
        s[ar[1] - 1].append(ar[0] - 1)
    return s


# x为记录入边的颜色
def dfs(currentNode, arr, x):
    m1 = childNodeNums
    m2 = childNodeStatus
    childNodes = arr[currentNode]
    if len(childNodes) == 0:
        return

    childNodeNums[currentNode] = childNodeNums[currentNode] + len(arr[currentNode])
    childNodeStatus[currentNode] = 1

    kk = 0
    for i, childNodeIndex in enumerate(childNodes):

        if childNodeStatus[childNodeIndex] == -1:
            kk = kk + 1
            if x == kk:
                kk = kk + 1
            nodeLines[currentNode].append(kk)
            dfs(childNodeIndex, arr, kk)




data = prepare(n, arr)
dfs(0, data, -1)

print(max(childNodeNums))

for ar in nodeLines:
    for r in ar:
        print(r)

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = []
for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

childNodeNums = [0 for i in range(n)]
childNodeStatus = [-1 for i in range(n)]
mm = []


def prepare(n, arr):
    s = [[] for i in range(n)]
    for ar in arr:
        s[ar[0] - 1].append(ar[1] - 1)
        s[ar[1] - 1].append(ar[0] - 1)
    return s


# x为记录入边的颜色
def dfs(currentNode, arr, x):
    m1 = childNodeNums
    m2 = childNodeStatus
    childNodes = arr[currentNode]
    if len(childNodes) == 0:
        return

    childNodeNums[currentNode] = childNodeNums[currentNode] + len(arr[currentNode])
    childNodeStatus[currentNode] = 1

    kk = 0
    for i, childNodeIndex in enumerate(childNodes):

        if childNodeStatus[childNodeIndex] == -1:
            kk = kk + 1
            if x == kk:
                kk = kk + 1
            mm.append(kk)
            dfs(childNodeIndex, arr, kk)




data = prepare(n, arr)
dfs(0, data, -1)

print(max(childNodeNums))

for m in mm:
    print(m)

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = []
for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])


childNodeNums = [0 for i in range(n)]
childNodeStatus = [-1 for i in range(n)]
nodeLines = [0 for i in range(n - 1)]


def prepare(n, arr):
    s = [[] for i in range(n)]
    for ar in arr:
        s[ar[0] - 1].append(ar[1] - 1)
        s[ar[1] - 1].append(ar[0] - 1)
    return s


# x为记录入边的颜色
def dfs(currentNode, arr, x):
    m1 = childNodeNums
    m2 = childNodeStatus
    childNodes = arr[currentNode]
    if len(childNodes) == 0:
        return

    childNodeNums[currentNode] = childNodeNums[currentNode] + len(arr[currentNode])
    childNodeStatus[currentNode] = 1

    kk = 0
    for i, childNodeIndex in enumerate(childNodes):

        if childNodeStatus[childNodeIndex] == -1:
            kk = kk + 1
            if x == kk:
                kk = kk + 1
            appendIntoNode(currentNode+1,childNodeIndex+1,kk)
            dfs(childNodeIndex, arr, kk)


def appendIntoNode(currentNode,childNode,value):
    for i,v in enumerate(arr):
        if (v[0] == currentNode and v[1] == childNode):
            nodeLines[i] = value


data = prepare(n, arr)
dfs(0, data, -1)

print(max(childNodeNums))

for ar in nodeLines:
    print(ar)

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = []
for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

childNodeNums = [0 for i in range(n)]
childNodeStatus = [-1 for i in range(n)]
nodeLines = [0 for i in range(n - 1)]


def prepare(n, arr):
    s = [[] for i in range(n)]
    for ar in arr:
        s[ar[0] - 1].append(ar[1] - 1)
        s[ar[1] - 1].append(ar[0] - 1)
    return s


# x为记录入边的颜色
def dfs(currentNode, arr, x):
    m1 = childNodeNums
    m2 = childNodeStatus
    childNodes = arr[currentNode]
    if len(childNodes) == 0:
        return

    childNodeNums[currentNode] = childNodeNums[currentNode] + len(arr[currentNode])
    childNodeStatus[currentNode] = 1

    kk = 0
    for i, childNodeIndex in enumerate(childNodes):

        if childNodeStatus[childNodeIndex] == -1:
            kk = kk + 1
            if x == kk:
                kk = kk + 1
            appendIntoNode(currentNode+1,childNodeIndex+1,kk)
            dfs(childNodeIndex, arr, kk)


def appendIntoNode(currentNode,childNode,value):
    for i,v in enumerate(arr):
        if (v[0] == currentNode and v[1] == childNode):
            nodeLines[i] = value


data = prepare(n, arr)
dfs(0, data, -1)

print(max(childNodeNums))

for ar in nodeLines:
    print(ar)

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = []
for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

childNodeNums = [0 for i in range(n)]
childNodeStatus = [-1 for i in range(n)]
nodeLineColors = [[] for i in range(n - 1)]


def prepare(n, arr):
    s = [[] for i in range(n)]
    for ar in arr:
        s[ar[0] - 1].append(ar[1] - 1)
        s[ar[1] - 1].append(ar[0] - 1)
    return s


# x为记录入边的颜色
def dfs(currentNode, arr, x):
    m1 = childNodeNums
    m2 = childNodeStatus
    childNodes = arr[currentNode]
    if len(childNodes) == 0:
        return

    childNodeNums[currentNode] = childNodeNums[currentNode] + len(arr[currentNode])
    childNodeStatus[currentNode] = 1

    kk = 0
    for i, childNodeIndex in enumerate(childNodes):

        if childNodeStatus[childNodeIndex] == -1:
            kk = kk + 1
            if x == kk:
                kk = kk + 1
            nodeLineColors[currentNode].append(kk)
            dfs(childNodeIndex, arr, kk)


data = prepare(n, arr)
dfs(0, data, -1)

print(max(childNodeNums))


for ar in nodeLineColors:
    for a in ar:
        print(a)

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = []
for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

childNodeNums = [0 for i in range(n)]
childNodeStatus = [-1 for i in range(n)]
nodeLineColors = [[] for i in range(n - 1)]


def prepare(n, arr):
    s = [[] for i in range(n)]
    for ar in arr:
        s[ar[0] - 1].append(ar[1] - 1)
        s[ar[1] - 1].append(ar[0] - 1)
    return s


# x为记录入边的颜色
def dfs(currentNode, arr, x):
    childNodes = arr[currentNode]
    if len(childNodes) == 0:
        return

    childNodeNums[currentNode] = childNodeNums[currentNode] + len(arr[currentNode])
    childNodeStatus[currentNode] = 1

    kk = 0
    for i, childNodeIndex in enumerate(childNodes):
        if x == i:
            kk = kk + 1
        if childNodeStatus[childNodeIndex] == -1:
            kk = kk + 1
            nodeLineColors[currentNode].append(kk)
            dfs(childNodeIndex, arr, i)


data = prepare(n, arr)
dfs(0, data, -1)

print(max(childNodeNums))
for ar in nodeLineColors:
    for a in ar:
        print(a)

Buggy submission (verdict: Runtime Error):
n = int(input())
arr = []
for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

childNodeNums = [0 for i in range(n)]
childNodeStatus = [-1 for i in range(n)]
nodeLineColors = [[] for i in range(n - 1)]


def prepare(n, arr):
    s = [[] for i in range(n)]
    for ar in arr:
        s[ar[0] - 1].append(ar[1] - 1)
        s[ar[1] - 1].append(ar[0] - 1)
    return s


# x为记录入边的颜色
def dfs(currentNode, arr, x):
    childNodes = arr[currentNode]
    if len(childNodes) == 0:
        return

    childNodeNums[currentNode] = childNodeNums[currentNode] + len(arr[currentNode])
    childNodeStatus[currentNode] = 1

    kk = 0
    for i, childNodeIndex in enumerate(childNodes):
        if x == i:
            kk = kk + 1
        if childNodeStatus[childNodeIndex] == -1:
            kk = kk + 1
            nodeLineColors[currentNode].append(kk)
            dfs(childNodeIndex, arr, i)


data = prepare(n, arr)
dfs(0, data, -1)

print(max(childNodeNums))
for ar in nodeLineColors:
    for a in ar:
        print(a)