Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03435 (python)

Fixed submission:
c = [list(map(int, input().split())) for _ in range(3)]

a = [0] * 3
b = [0] * 3
ans = "Yes"
for a1 in range(101):
    a[0] = a1
    b[0] = c[0][0] - a1
    b[1] = c[0][1] - a1
    b[2] = c[0][2] - a1
    a[1] = c[1][0] - b[0]
    a[2] = c[2][0] - b[0]
    for i in range(3):
        for j in range(3):
            if a[i] + b[j] != c[i][j]:
                ans = "No"

print(ans)

Buggy submission (verdict: Runtime Error):
c = [list(map(int, input().split())) for _ in range(3)]

a = [0] * 3
b = [0] * 3
ans = "Yes"
a[0] = 0
b[0] = c[0][0] - a1
b[1] = c[0][1] - a1
b[2] = c[0][2] - a1
a[1] = c[1][0] - b[0]
a[2] = c[2][0] - b[0]
for i in range(3):
    for j in range(3):
        if a[i] + b[j] != c[i][j]:
            ans = "No"

print(ans)
Problem p03478 (python)

Fixed submission:
n, a, b = map(int, input().split())

ans = 0
for num in range(1, n + 1):
    s = str(num)
    sm = 0
    for let in s:
        sm += int(let)
    if a <= sm <= b:
        ans += num

print(ans)

Buggy submission (verdict: Runtime Error):
n, a, b = map(int, input().split())

ans = 0
for num in range(1, n + 1):
    s = str(num)
    sm = 0
    for let in s:
        sm += int(let)
    if a <= sm <= b:
        ans += num

print(cnt)
Problem p03488 (python)

Fixed submission:
s = input()
x, y = map(int, input().split())

mv = [len(e) for e in s.split("T")]
mv_x = mv[2::2]
mv_y = mv[1::2]


def solve_dp(li, start, end):
    l = len(li)
    mx = sum(li)

    if l == 0:
        return start == end

    left = start - mx
    right = start + mx

    if end > right or end < left:
        return 0

    dp = [[0] * (2 * mx + 1) for _ in range(l + 1)]
    dp[0][start] = 1
    for i, e in enumerate(li, 1):
        for j in range(left, right + 1):
            if left <= j - e <= right:
                dp[i][j] |= dp[i-1][j-e]
            if left <= j + e <= right:
                dp[i][j] |= dp[i-1][j+e]

    return dp[l][end]


bl_x = solve_dp(mv_x, mv[0], x)
bl_y = solve_dp(mv_y, 0, y)

if bl_x and bl_y:
    ans = "Yes"
else:
    ans = "No"

print(ans)

Buggy submission (verdict: Runtime Error):
s = input()
x, y = map(int, input().split())

mv = [len(e) for e in s.split("T")]
mv_x = mv[2::2]
mv_y = mv[1::2]


def solve_dp(li, start, end):
    l = len(li)
    mx = sum(li)

    left = start - mx
    right = start + mx

    if end > right or end < left:
        return 0

    dp = [[0] * (2 * mx + 1) for _ in range(l + 1)]
    dp[0][start] = 1
    for i, e in enumerate(li, 1):
        for j in range(left, right + 1):
            if left <= j - e <= right:
                dp[i][j] |= dp[i-1][j-e]
            if left <= j + e <= right:
                dp[i][j] |= dp[i-1][j+e]

    return 0#dp[l][end]


bl_x = solve_dp(mv_x, mv[0], x)
bl_y = solve_dp(mv_y, 0, y)

if bl_x and bl_y:
    ans = "Yes"
else:
    ans = "No"

print(ans)

Buggy submission (verdict: Runtime Error):
s = input()
x, y = map(int, input().split())

mv = [len(e) for e in s.split("T")]
mv_x = mv[2::2]
mv_y = mv[1::2]


def solve_dp(li, start, end):
    l = len(li)
    mx = sum(li)

    left = start - mx
    right = start + mx

    if end > right or end < left:
        return 0

    dp = [[0] * (2 * mx + 1) for _ in range(l + 1)]
    dp[0][start] = 1
    for i, e in enumerate(li, 1):
        for j in range(left, right + 1):
            if left <= j - e <= right:
                dp[i][j] |= dp[i-1][j-e]
            if left <= j + e <= right:
                dp[i][j] |= dp[i-1][j+e]

    return dp[l][end]


bl_x = solve_dp(mv_x, mv[0], x)
bl_y = solve_dp(mv_y, 0, y)

if bl_x and bl_y:
    ans = "Yes"
else:
    ans = "No"

print(ans)

Buggy submission (verdict: Runtime Error):
s = input()
x, y = map(int, input().split())

mv = [len(e) for e in s.split("T")]
mv_x = mv[2::2]
mv_y = mv[1::2]


def solve_dp(li, start, end):
    l = len(li)
    mx = sum(li)
    
    if end > start + mx or end < start - mx:
        return 0

    dp = [[0] * (2 * mx + 1) for _ in range(l + 1)]
    dp[0][start] = 1
    for i, e in enumerate(li, 1):
        for j in range(-mx + start, mx + start + 1):
            if -mx <= j - e <= mx:
                dp[i][j] |= dp[i-1][j-e]
            if -mx <= j + e <= mx:
                dp[i][j] |= dp[i-1][j+e]

    return dp[l][end]


bl_x = solve_dp(mv_x, mv[0], x)
bl_y = solve_dp(mv_y, 0, y)

if bl_x and bl_y:
    ans = "Yes"
else:
    ans = "No"

print(ans)
Problem p03579 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 6)

n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]

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

import sys

sys.setrecursionlimit(10 ** 6)


class Tree:
    WHITE = 0
    GRAY = -1
    BLACK = 1

    def __init__(self, adj):
        n = len(adj)
        self.adj = adj
        self.colors = [self.WHITE] * n
        self.depths = [-1] * n
        self.depth = 0

    def init(self):
        self.__init__(self.adj)

    def dfs(self, u):
        if self.colors[u] == self.BLACK:
            return

        self.colors[u] = self.GRAY
        self.depths[u] = self.depth

        for v, w in self.adj[u]:
            if self.colors[v] == self.WHITE:
                self.depth += w
                self.dfs(v)
                self.depth -= w

        self.colors[u] = self.BLACK

    def bipartite_graph(self, u, c):
        self.colors[u] = c
        for v in self.adj[u]:
            if self.colors[v] == c:
                return False
            if self.colors[v] == self.WHITE and not self.bipartite_graph(v, -c):
                return False

        return True


tree = Tree(g)
if tree.bipartite_graph(0, tree.GRAY):
    black = tree.colors.count(tree.GRAY)
    white = tree.colors.count(tree.BLACK)
    ans = black * white - m

else:
    ans = n * (n - 1) // 2 - m

print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 6)

n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]

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


class Tree:
    WHITE = 0
    GRAY = 1
    BLACK = 2

    def __init__(self, adj):
        n = len(adj)
        self.adj = adj
        self.colors = [self.WHITE] * n
        self.depths = [-1] * n
        self.depth = 0

    def init(self):
        self.__init__(self.adj)

    def dfs(self, u):
        if self.colors[u] == self.BLACK:
            return

        self.colors[u] = self.GRAY
        self.depths[u] = self.depth

        for v in self.adj[u]:
            if self.colors[v] == self.WHITE:
                self.depth += 1
                self.dfs(v)
                self.depth -= 1
            elif (self.depths[u] - self.depths[v]) % 2 == 0:
                return False

        self.colors[u] = self.BLACK

        return True


tree = Tree(g)
if tree.dfs(0):
    black = len([x for x in tree.depths if x % 2])
    white = len([x for x in tree.depths if x % 2 == 0])
    ans = black * white - m

else:
    raise Exception
    ans = n * (n - 1) // 2 - m

print(ans)
Problem p03600 (python)

Fixed submission:
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix

import numpy as np

n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]

a = np.array(a)

g = csr_matrix(a)
dist = floyd_warshall(g)

bl = dist == a

if bl.all():
    sm = dist.sum()
    INF = 10 ** 18 + 1
    inf = np.array([INF] * n)
    inf = np.diag(inf)
    dist += inf
    for u in range(n):
        for v in range(n):
            if u == v:
                continue

            mn = np.min(dist[u] + dist[v])
            if mn == dist[u, v]:
                sm -= dist[u, v]

    ans = int(sm) // 2
    print(ans)

else:
    print(-1)

Buggy submission (verdict: Runtime Error):
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix

import numpy as np

n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]

a = np.array(a)

g = csr_matrix(a)
dist = floyd_warshall(g)

bl = dist == a

if bl.all():
    sm = dist.sum()
    for u in range(n):
        for v in range(n):
            if u == v:
                continue
            rmv = [u, v]
            i_rmv = np.ones(n, dtype=bool)
            i_rmv[rmv] = False

            mn = np.min(dist[u, i_rmv] + dist[v, i_rmv])
            if mn == dist[u, v]:
                sm -= dist[u, v]

    ans = int(sm) // 2
    print(ans)

else:
    print(-1)
Problem p03611 (python)

Fixed submission:
# -*- coding: utf-8 -*-

from collections import Counter

n = int(input())
a = list(map(int, input().split()))

c = Counter(a)
keys = c.keys()

st = set()
for key in keys:
    for i in (-1, 0, 1):
        st.add(key + i)

ans = 0
for x in st:
    cnt = c[x-1] + c[x] + c[x+1]
    ans = max(ans, cnt)

print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

n = int(input())
a = list(map(int, input().split()))

a.sort()
l = 0
r = 0
ans = 0
cnt = [0, 0, 0]
while l < n and r < n:
    a_l = {a[l] - 1, a[l], a[l] + 1}
    a_r = {a[r] - 1, a[r], a[r] + 1}
    l_r = a_l & a_r

    if l_r:
        for ele in l_r:
            diff = ele - a[l]
            if diff == 0:
                cnt[1] += 1
            elif diff < 0:
                cnt[0] += 1
            else:
                cnt[2] += 1
        r += 1

    else:
        ans = max(ans, max(cnt))
        l += 1
        if l < n:
            diff = a[l] - a[l-1]
            if diff == 0:
                cnt = cnt
            elif diff == 1:
                cnt = cnt[1:] + [0]
            elif diff == 2:
                cnt = cnt[2] + [0, 0]
            else:
                cnt = [0, 0, 0]

print(ans)
Problem p03633 (python)

Fixed submission:
# -*- coding: utf-8 -*-


def gcd(x, y):
    while x % y:
        x, y = y, x % y
    return y


def lcm(x, y):
    return x * y // gcd(x, y)


n = int(input())
t = [int(input()) for _ in range(n)]

ans = 1
for ele in t:
    ans = lcm(ans, ele)

print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

from numpy import lcm


n = int(input())
t = [int(input()) for _ in range(n)]

ans = 1
for ele in t:
    ans = lcm(ans, ele)

print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

from math import gcd


def lcm(x, y):
    return x * y // gcd(x, y)


n = int(input())
t = [int(input()) for _ in range(n)]

ans = 1
for ele in t:
    ans = lcm(ans, ele)

print(ans)
Problem p03680 (python)

Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]

pushed = set()
cnt = 0
push= 1
while True:
    if push in pushed:
        cnt = -1
        break
    pushed.add(push)
    push = a[push-1]
    cnt += 1
    if push == 2:
        break

print(cnt)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = [int(input()) for _ in range(n)]

pushed = set()
cnt = 0
push= 1
while True:
    if push in pushed:
        cnt = -1
        break
    pushed.add(push)
    push = a[push]
    cnt += 1
    if push == 2:
        break

print(cnt)
Problem p03682 (python)

Fixed submission:
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse import csr_matrix
from operator import itemgetter

n = int(input())
xy = [list(map(int, input().split())) + [i] for i in range(n)]

edge = set()
for key in (itemgetter(0), itemgetter(1)):
    xy.sort(key=key)
    for (x1, y1, i1), (x2, y2, i2) in zip(xy, xy[1:]):
        d = min(abs(x1 - x2), abs(y1 - y2))
        edge.add((i1, i2, d))

u, v, c = zip(*edge)

graph = csr_matrix((c, (u, v)), shape=(n, n), dtype=int)
mst = minimum_spanning_tree(graph, overwrite=True).astype(int)
ans = mst.sum()
print(ans)

Buggy submission (verdict: Runtime Error):
from operator import itemgetter
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse import csr_matrix

n = int(input())
xy = [list(map(int, input().split())) for _ in range(n)]

xyi = [[x, y, i] for i, (x, y) in enumerate(xy)]
row = []
col = []
cost = []

xyi.sort()
for (x1, y1, i1), (x2, y2, i2) in zip(xyi, xyi[1:]):
    row.append(i1)
    col.append(i2)
    cost.append(min(abs(x1 - x2), abs(y1 - y2)))

xyi.sort(key=itemgetter(1))
for (x1, y1, i1), (x2, y2, i2) in zip(xyi, xyi[1:]):
    row.append(i1)
    col.append(i2)
    cost.append(min(abs(x1 - x2), abs(y1 - y2)))

graph = csr_matrix((cost, (row, col)))
mst = minimum_spanning_tree(graph)
ans = mst.sum().astype(int)
print(ans)

Buggy submission (verdict: Runtime Error):
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse import csr_matrix
import numpy as np

n = int(input())
xy = [list(map(int, input().split())) for _ in range(n)]

x = []
y = []
for i, (ex, ey) in enumerate(xy):
    x.append([ex, i])
    y.append([ey, i])

x.sort()
y.sort()

u = []
v = []
c = []


def f(li):
    for (e1, i1), (e2, i2) in zip(li, li[1:]):
        dist = e2 - e1
        u.append(i1)
        v.append(i2)
        c.append(dist)


f(x)
f(y)

graph = csr_matrix((c, (u, v)))
mst = minimum_spanning_tree(graph).astype(np.int64)
ans = mst.sum()
print(ans)

Buggy submission (verdict: Runtime Error):
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse import csr_matrix

n = int(input())
xy = [list(map(int, input().split())) for _ in range(n)]

x = []
y = []
for i, (ex, ey) in enumerate(xy):
    x.append([ex, i])
    y.append([ey, i])

x.sort()
y.sort()

u = []
v = []
c = []


def f(li):
    for (e1, i1), (e2, i2) in zip(li, li[1:]):
        dist = e2 - e1
        u.append(i1)
        v.append(i2)
        c.append(dist)


f(x)
f(y)

graph = csr_matrix((c, (u, v)))
mst = minimum_spanning_tree(graph)
ans = int(mst.sum())
print(ans)
Problem p03720 (python)

Fixed submission:
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]

cnt = [0] * n

for i in range(m):
    cnt[ab[i][0] - 1] += 1
    cnt[ab[i][1] - 1] += 1

for i in range(n):
    print(cnt[i])

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]

cnt = [0] * n

for i in range(m):
    cnt[ab[i][0] - 1] += 1
    cnt[ab[i][1] - 1] += 1

for i in range(n):
    print(cnt(i))
Problem p03732 (python)

Fixed submission:
n, W = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(n)]

dp = [[[-1] * (3 * n + 1) for _ in range(n + 1)] for _ in range(n + 1)]
dp[0][0][0] = 0
w1 = wv[0][0]

for i, (w, v) in enumerate(wv):
    for j in range(i + 1):
        for k in range(3 * n + 1):
            if dp[i][j][k] != -1:
                dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
                w_diff = w - w1
                if j + 1 <= n and k + w_diff <= 3 * n:
                    dp[i+1][j+1][k+w_diff] = max(dp[i+1][j+1][k+w_diff], dp[i][j][k] + v)

ans = 0
for j in range(n + 1):
    w_diff_mx = min(W - w1 * j, 3 * n)
    for k in range(w_diff_mx + 1):
        ans = max(ans, dp[n][j][k])

print(ans)

Buggy submission (verdict: Runtime Error):
n, W = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(n)]

dp = [[[-1] * (3 * n + 1) for _ in range(n + 1)] for _ in range(n + 1)]
dp[0][0][0] = 0
w1 = wv[0][0]

for i, (w, v) in enumerate(wv):
    for j in range(i + 1):
        for k in range(3 * n + 1):
            if dp[i][j][k] != -1:
                dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
                w_diff = w - w1
                if j + 1 <= n and k + w_diff <= 3 * n:
                    dp[i+1][j+1][k+w_diff] = max(dp[i+1][j+1][k+w_diff], dp[i][j][k] + v)

ans = 0
for j in range(n + 1):
    w_diff_mx = W - w1 * j
    for k in range(w_diff_mx + 1):
        ans = max(ans, dp[n][j][k])

print(ans)
Problem p03780 (python)

Fixed submission:
import numpy as np


def check(x):
    dp = np.zeros(k, np.bool)
    dp[0] = True
    for e in a[:x] + a[x+1:]:
        dp[e:] |= dp[:-e]

    return dp[-a[x]:].any()


n, k = map(int, input().split())
a = list(map(int, input().split()))

a.sort()

lb = -1
ub = n
while ub - lb > 1:
    m = (lb + ub) // 2
    if check(m):
        ub = m
    else:
        lb = m

print(ub)

Buggy submission (verdict: Runtime Error):
import numpy as np
from numba import njit

n, k = map(int, input().split())
a = tuple(map(int, input().split()))

dp = np.zeros((n + 1, k), bool)
dp[0][0] = True
for i, e in enumerate(a, 1):
    dp[i] = dp[i-1]
    dp[i, e:] |= dp[i-1, :-e]

dp2 = np.zeros((n + 1, k), bool)
dp2[0][0] = True
for i, e in enumerate(a[::-1], 1):
    dp2[i] = dp2[i-1]
    dp2[i, e:] |= dp2[i-1, :-e]


@njit
def f(a, dp, dp2):
    ret = 0
    for i, e in enumerate(a, 1):
        li = [j for j in range(k) if dp[i-1][j]]
        li2 = [j for j in range(k) if dp2[n-i][j]]

        j = len(li2) - 1
        mx = 0
        for e2 in li:
            while j > 0 and e2 + li2[j] >= k:
                j -= 1

            if e2 + li2[j] < k:
                mx = max(mx, e2 + li2[j])

        if mx + e < k:
            ret += 1

    return ret


ans = f(a, dp, dp2)
print(ans)

Buggy submission (verdict: Runtime Error):
from numba import njit


@njit
def solve(n, k, a):
    dp = [[0] * k for _ in range(n + 1)]
    dp[0][0] = 1
    for i, e in enumerate(a, 1):
        for j in range(k):
            dp[i][j] = dp[i-1][j]

        for j in range(k - e):
            if dp[i-1][j]:
                dp[i][j+e] = 1

    dp2 = [[0] * k for _ in range(n + 1)]
    dp2[0][0] = 1
    for i, e in enumerate(a[::-1], 1):
        for j in range(k):
            dp2[i][j] = dp2[i-1][j]

        for j in range(k - e):
            if dp2[i-1][j]:
                dp2[i][j+e] = 1

    ans = 0
    for i, e in enumerate(a, 1):
        li = [j for j in range(k) if dp[i-1][j]]
        li2 = [j for j in range(k) if dp2[n-i][j]]

        j = len(li2) - 1
        mx = 0
        for e2 in li:
            while j > 0 and e2 + li2[j] >= k:
                j -= 1

            if e2 + li2[j] < k:
                mx = max(mx, e2 + li2[j])

        if mx + e < k:
            ans += 1

    print(ans)


if __name__ == "__main__":
    n, k = map(int, input().split())
    a = tuple(map(int, input().split()))
    solve(n, k, a)
Problem p03838 (python)

Fixed submission:
def main():
    x, y = map(int, input().split())

    if 0 <= x <= y or x <= y <= 0:
        ans = abs(abs(x) - abs(y))

    elif x <= 0 <= y or y <= 0 <= x:
        ans = abs(abs(x) - abs(y)) + 1

    elif y < x < 0 or 0 < y < x:
        ans = abs(abs(x) - abs(y)) + 2

    print(ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    x, y = map(int, input().split())

    if 0 <= x <= y or x <= y <= 0:
        ans = abs(abs(x) - abs(y))

    elif x < 0 < y or y < 0 < x:
        ans = abs(abs(x) - abs(y)) + 1

    elif y < x < 0 or 0 < y < x:
        ans = abs(abs(x) - abs(y)) + 2

    print(ans)


if __name__ == "__main__":
    main()
Problem p03866 (python)

Fixed submission:
from math import sqrt
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix

xs, ys, xt, yt = map(int, input().split())
n = int(input())
xyr = [list(map(int, input().split())) for _ in range(n)]

xyr.append([xs, ys, 0])
xyr.append([xt, yt, 0])

row = []
col = []
cost = []

for i, (xi, yi, ri) in enumerate(xyr):
    for j, (xj, yj, rj) in enumerate(xyr[i+1:], i+1):
        row.append(i)
        col.append(j)
        dist = max(0, sqrt((xi - xj) ** 2 + (yi - yj) ** 2) - ri - rj)
        cost.append(dist)

g = csr_matrix((cost, (row, col)), shape=(n + 2, n + 2))
d = dijkstra(g, directed=False, indices=n)
ans = d[n+1]
print(ans)

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

import numpy as np
from scipy.spatial.distance import cdist
from scipy.sparse.csgraph import dijkstra, csgraph_from_dense

xs, ys, xt, yt = map(int, readline().split())
n = int(readline())
xyr = np.array(read().split(), np.int64)
xyr = np.append(xyr, [xs, ys, 0, xt, yt, 0])
xyr = xyr.reshape((-1, 3))

xy = xyr[:, :2]
dist = cdist(xy, xy)
r = xyr[:, 2]
dist = np.maximum(dist - r - r.reshape((-1, 1)), 0)
g = csgraph_from_dense(dist, null_value=np.inf)

ans = dijkstra(g, indices=n)[n+1]
print(ans)
Problem p03921 (python)

Fixed submission:
class UnionFind:
    def __init__(self, n):
        self.n = n
        self.p = [e for e in range(n)]
        self.rank = [0] * n
        self.size = [1] * n

    def same(self, u, v):
        return self.find_set(u) == self.find_set(v)

    def unite(self, u, v):
        u = self.find_set(u)
        v = self.find_set(v)

        if u == v:
            return

        if self.rank[u] > self.rank[v]:
            self.p[v] = u
            self.size[u] += self.size[v]
        else:
            self.p[u] = v
            self.size[v] += self.size[u]
            if self.rank[u] == self.rank[v]:
                self.rank[v] += 1

    def find_set(self, u):
        if u != self.p[u]:
            self.p[u] = self.find_set(self.p[u])

        return self.p[u]

    def update_p(self):
        for u in range(self.n):
            self.find_set(u)

    def get_size(self, u):
        return self.size[self.find_set(u)]


n, m = map(int, input().split())
l = []
for _ in range(n):
    k, *ls = map(int, input().split())
    l.append(ls)

uf = UnionFind(m)

for li in l:
    for e1, e2 in zip(li, li[1:]):
        e1 -= 1
        e2 -= 1
        uf.unite(e1, e2)

p = uf.find_set(l[0][0] - 1)
ans = "YES"
for e, *_ in l:
    if uf.find_set(e - 1) != p:
        ans = "NO"

print(ans)

Buggy submission (verdict: Runtime Error):
class UnionFind:
    def __init__(self, n):
        self.n = n
        self.p = [e for e in range(n)]
        self.rank = [0] * n
        self.size = [1] * n

    def same(self, u, v):
        return self.find_set(u) == self.find_set(v)

    def unite(self, u, v):
        u = self.find_set(u)
        v = self.find_set(v)

        if u == v:
            return

        if self.rank[u] > self.rank[v]:
            self.p[v] = u
            self.size[u] += self.size[v]
        else:
            self.p[u] = v
            self.size[v] += self.size[u]
            if self.rank[u] == self.rank[v]:
                self.rank[v] += 1

    def find_set(self, u):
        if u != self.p[u]:
            self.p[u] = self.find_set(self.p[u])

        return self.p[u]

    def update_p(self):
        for u in range(self.n):
            self.find_set(u)

    def get_size(self, u):
        return self.size[self.find_set(u)]


n, m = map(int, input().split())
l = []
for _ in range(n):
    k, *ls = map(int, input().split())
    l.append(ls)

uf = UnionFind(m)

for li in l:
    for e1, e2 in zip(li, li[1:]):
        e1 -= 1
        e2 -= 1
        uf.unite(e1, e2)

p = uf.find_set(l[0][0])
ans = "YES"
for e, *_ in l:
    e -= 1
    if uf.find_set(e) != p:
        ans = "NO"

print(ans)
Problem p04000 (python)

Fixed submission:
from collections import defaultdict

h, w, n = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]

d = defaultdict(int)

for a, b in ab:
    for hi in range(a - 2, a + 1):
        for wi in range(b - 2, b + 1):
            if 1 <= hi <= h - 2 and 1 <= wi <= w - 2:
                d[(hi, wi)] += 1

cnt = [0] * 10
for v in d.values():
    cnt[v] += 1

cnt[0] = (h - 2) * (w - 2) - sum(cnt[1:])
print(*cnt, sep="\n")

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

h, w, n = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]

d = defaultdict(int)

for a, b in ab:
    for hi in range(a - 2, a + 1):
        for wi in range(b - 2, b + 1):
            if 1 <= hi <= h - 2 and 1 <= wi <= w - 2:
                d[(hi, wi)] += 1

cnt = [0] * 9
for v in d.values():
    cnt[v] += 1

cnt[0] = (h - 2) * (w - 2) - sum(cnt[1:])
print(*cnt, sep="\n")