Problem p02850 (python)

Fixed submission:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

from collections import deque
        
n = int(input())
uf = UnionFind(n)
g = [[] for i in range(n)]
cnt = [0] * n
ans = [0] * (n - 1)

for i in range(n - 1):
    a, b = map(lambda x:int(x) - 1, input().split())
    uf.union(a, b)
    g[a].append((b, i))
    g[b].append((a, i))
    cnt[a] += 1
    cnt[b] += 1
    

for i in uf.roots():
    visited = [0] * n
    visited[i] = 1
    que = deque()
    que.append([i, 0])
    while que:
        s = que.popleft()
        k = 0
        for j in g[s[0]]:
            if not visited[j[0]]:
                while True:
                    k += 1
                    if k != s[1]:
                        que.append([j[0], k])
                        visited[j[0]] = 1
                        ans[j[1]] = k
                        break

print(max(cnt))
print(*ans, sep = "\n")

Buggy submission (verdict: Runtime Error):
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

from collections import deque
        
n = int(input())
uf = UnionFind(n)
g = [[] for i in range(n)]
cnt = [0] * n
ans = [[0] * n for i in range(n)]
ans_index = []

for i in range(n - 1):
    a, b = map(lambda x:int(x) - 1, input().split())
    uf.union(a, b)
    g[a].append(b)
    g[b].append(a)
    cnt[a] += 1
    cnt[b] += 1
    ans_index.append((a, b))
    

for i in uf.roots():
    visited = [0] * n
    visited[i] = 1
    que = deque()
    que.append([i, 0])
    while que:
        s = que.popleft()
        k = 0
        for j in g[s[0]]:
            if not visited[j]:
                while True:
                    k += 1
                    if k != s[1]:
                        que.append([j, k])
                        visited[j] = 1
                        ans[s[0]][j] = k
                        ans[j][s[0]] = k
                        break

print(max(cnt))
for i in range(n - 1):
    print(ans[ans_index[i][0]][ans_index[i][1]])

Buggy submission (verdict: Runtime Error):
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

from collections import deque
        
n = int(input())
uf = UnionFind(n)
g = [[] for i in range(n)]
cnt = [0] * n
ans = [0] * (n - 1)
ans_index = []

for i in range(n - 1):
    a, b = map(lambda x:int(x) - 1, input().split())
    uf.union(a, b)
    g[a].append(b)
    g[b].append(a)
    cnt[a] += 1
    cnt[b] += 1
    ans_index.append((a, b))
    

for i in uf.roots():
    visited = [0] * n
    visited[i] = 1
    que = deque()
    que.append([i, 0])
    while que:
        s = que.popleft()
        k = 0
        for j in g[s[0]]:
            if not visited[j]:
                while True:
                    k += 1
                    if k != s[1]:
                        que.append([j, k])
                        visited[j] = 1
                        ans[ans_index.index((s[0], j))] = k
                        break

print(max(cnt))
print(*ans, sep = "\n")

Buggy submission (verdict: Runtime Error):
n = int(input())
cnt = [0] * n
ok = [[0] * (n - 1) for i in range(n)]
ans = []
for i in range(n - 1):
    a, b = map(lambda x: int(x) - 1, input().split())
    for j in range(n):
        if not ok[a][j] and not ok[b][j]:
            ans.append(j + 1)
            ok[a][j] = 1
            ok[b][j] = 1
            break
    cnt[a] += 1
    cnt[b] += 1
    
print(max(cnt))
print(*ans, sep = "\n")

Buggy submission (verdict: Runtime Error):
n = int(input())
cnt = [0] * n
ok = [[0] * (n - 1) for i in range(n)]
ans = []
for i in range(n - 1):
    a, b = map(lambda x: int(x) - 1, input().split())
    for j in range(n - 1):
        if not ok[a][j] and not ok[b][j]:
            ans.append(j + 1)
            ok[a][j] = 1
            ok[b][j] = 1
            break
    cnt[a] += 1
    cnt[b] += 1
    
print(max(cnt))
print(*ans, sep = "\n")