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

Fixed submission:
def main():
    N, P = map( int ,input().split())
    S = [ int(s) for s in list(input())][::-1]
    if P == 2 or P == 5:
        ans = 0
        for i in range(N):
            if S[i]%P == 0:
                ans += N-i
        print(ans)
        return
    L = [0]*P
    L[0] = 1
    t = 0
    for i in range(N):
        t = (S[i]*pow(10,i,P)+t)%P
        L[t] += 1
    ans = 0
    for l in L:
        ans += l*(l-1)//2
    print(ans)
    
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
def main():
    N, P = map( int ,input().split())
    S = [ int(s) for s in list(input())][::-1]
    if P == 2 or P == 5:
        ans = 0
        for i in range(N):
            if S[i]%P == 0:
                ans += N-i
        print(ans)
        return
    L = [0]*P
    L[0] = 1
    t = 0
    s = 1
    for i in range(N):
        t = (S[i]*s+t)%P
        L[t] += 1
        s = s*10%P
    ans = 0
    for l in L:
        ans += l*(l-1)//2
    print(ans)
    
if __name__ == '__main__':
    main()
Problem p02815 (python)

Fixed submission:
def main():
    N = int( input())
    Q = 10**9+7
    C = list( map( int, input().split()))
    C.sort()
    ans = 1
    if N == 1:
        print(C[0]*2%Q)
        return
    for i in range(N-1):
        ans += pow(2,i,Q)*(((N-1)-i)*pow(2,N-2-i,Q) + pow(2, N-1-i, Q))*C[i]%Q
        ans %= Q
    ans += pow(2, N-1, Q)*C[N-1]%Q
    ans -= 1
    print(ans*pow(2,N,Q)%Q)
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return(a)

def lcm(a, b):
    return a*b//gcd(a,b)
#import sys
#input = sys.stdin.readline
def main():
    N, M = map( int, input().split())
    A = list( map( int, input().split()))
    B = [a for a in A]
    T = 1
    L = 1
    C = [0]*N
    for i in range(N):
        for _ in range(30):
            if B[i]%2 == 0:
                B[i] //= 2
                C[i] += 1
            else:
                break
    p = C[0]
    for i in range(N-1):
        if p != C[i+1]:
            print(0)
            return
    q = pow(2,p)
    t = 1
    for i in range(N):
        t = lcm(t,A[i]//2)
    if t > M:
        print(0)
        return

    print((M-t)//t + 1)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return(a)

def lcm(a, b):
    return a*b//gcd(a,b)
#import sys
#input = sys.stdin.readline
def main():
    N, M = map( int, input().split())
    A = list( map( int, input().split()))
    B = [a for a in A]
    T = 1
    L = 1
    C = [0]*N
    for i in range(N):
        for _ in range(30):
            if B[i]%2 == 0:
                B[i] //= 2
                C[i] += 1
            else:
                break
    p = C[0]
    for i in range(N-1):
        if p != C[i+1]:
            print(0)
            return
    q = pow(2,p)
    t = 1
    for i in range(N):
        t = lcm(t,A[i]//2)
    if t > M:
        print(0)
        return

    print((M-t)//t + 1)

if __name__ == '__main__':
    main()
Problem p03034 (python)

Fixed submission:
from collections import defaultdict
N = int( input())
S = list( map( int, input().split()))

ans = 0
for c in range(1,N):
    now = 0
    for t in range(c ,N, c):
        if ((N-1-t)%c == 0 and N-1-t <= t) or N-1-t - c <=0:
            break
        now += S[t] + S[N-1-t]
        if now > ans:
            ans = now
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
N = int( input())
S = list( map( int, input().split()))

ans = 0
for c in range(1,N):
    now = 0
    k = 1
    for _ in range(1,N):
        if k*2 + 1 > N-2:
            break
        if k*c >= N-1:
            break
        if (N-1-k*c)%c <= k:
            break
        now += S[k*c] + S[N-1-k*c]
        d[k*c] = 1
        d[N-1-k*c] = 1
        if now > ans:
            ans = now
        k += 1
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque
N = int( input())
S = list( map( int, input().split()))

ans = 0
for c in range(1,N):
    now = 0
    k = 1
    for _ in range(1,N):
        if k*2 + 1 > N-2:
            break
        if k*c >= N-1:
            break
        if k*c >= N-1-k*c:
            break
        now += S[k*c] + S[N-1-k*c]
        H[k*c] = 1
        H[N-1-k*c] = 1
        if now > ans:
            ans = now
        k += 1
print(ans)
Problem p03242 (python)

Fixed submission:
n = input()
ans = ''
for x in n:
  if x == '1':
    ans += '9'
  else:
    ans += '1'
print(ans)

Buggy submission (verdict: Runtime Error):
n = input()
ans = ''
for x in n:
  if x == '1':
    ans += '9'
   else:
    ans += '1'
print(ans)
Problem p03291 (python)

Fixed submission:
S = input()
L = len(S)
Q = 10**9 + 7
anum = [ 0 for _ in range(L+1)]
#bnum = [ 0 for _ in range(L+1)]
cnum = [ 0 for _ in range(L+1)]
hnum = [ 0 for _ in range(L+1)]
for i in range(1,L+1):
    if S[i-1] == 'A':
        anum[i] = anum[i-1] + 1
#        bnum[i] = bnum[i-1]
        cnum[i] = cnum[i-1]
        hnum[i] = hnum[i-1]
    elif S[i-1] == 'B':
        anum[i] = anum[i-1]
#        bnum[i] = bnum[i-1] + 1
        cnum[i] = cnum[i-1]
        hnum[i] = hnum[i-1]
    elif S[i-1] == 'C':
        anum[i] = anum[i-1]
#        bnum[i] = bnum[i-1]
        cnum[i] = cnum[i-1] + 1
        hnum[i] = hnum[i-1]
    else:
        anum[i] = anum[i-1]
#        bnum[i] = bnum[i-1]
        cnum[i] = cnum[i-1]
        hnum[i] = hnum[i-1] + 1

ans = 0
czen = cnum[L]
hzen = hnum[L] 
for i in range(1,L+1):
    if S[i-1] == 'B' or S[i-1] == '?':
#        A = ((anum[i-1]*(3**hnum[i-1]))%Q + (hnum[i-1]*(3**(hnum[i-1]-1)))%Q)%Q
#        C = (((czen - cnum[i])*(3**(hzen - hnum[i])))%Q + ((hzen - hnum[i])*(3**(hzen - hnum[i]-1)))%Q)%
        A = ((anum[i-1]*pow(3,hnum[i-1],Q))%Q + (hnum[i-1]*pow(3,max(0,hnum[i-1]-1),Q))%Q)%Q
        C = (((czen - cnum[i])*pow(3,hzen - hnum[i],Q))%Q + ((hzen - hnum[i])*pow(3,max(0,hzen - hnum[i]-1),Q))%Q)%Q
        K = (A*C)%Q
        ans = (ans + K)%Q
print(int(ans))

Buggy submission (verdict: Runtime Error):
S = input()
L = len(S)
Q = 10**9 + 7
anum = [ 0 for _ in range(L+1)]
#bnum = [ 0 for _ in range(L+1)]
cnum = [ 0 for _ in range(L+1)]
hnum = [ 0 for _ in range(L+1)]
for i in range(1,L+1):
    if S[i-1] == 'A':
        anum[i] = anum[i-1] + 1
#        bnum[i] = bnum[i-1]
        cnum[i] = cnum[i-1]
        hnum[i] = hnum[i-1]
    elif S[i-1] == 'B':
        anum[i] = anum[i-1]
#        bnum[i] = bnum[i-1] + 1
        cnum[i] = cnum[i-1]
        hnum[i] = hnum[i-1]
    elif S[i-1] == 'C':
        anum[i] = anum[i-1]
#        bnum[i] = bnum[i-1]
        cnum[i] = cnum[i-1] + 1
        hnum[i] = hnum[i-1]
    else:
        anum[i] = anum[i-1]
#        bnum[i] = bnum[i-1]
        cnum[i] = cnum[i-1]
        hnum[i] = hnum[i-1] + 1

ans = 0
czen = cnum[L]
hzen = hnum[L] 
for i in range(1,L+1):
    if S[i-1] == 'B' or S[i-1] == '?':
#        A = ((anum[i-1]*(3**hnum[i-1]))%Q + (hnum[i-1]*(3**(hnum[i-1]-1)))%Q)%Q
#        C = (((czen - cnum[i])*(3**(hzen - hnum[i])))%Q + ((hzen - hnum[i])*(3**(hzen - hnum[i]-1)))%Q)%
        A = ((anum[i-1]*pow(3,hnum[i-1],Q))%Q + (hnum[i-1]*pow(3,hnum[i-1]-1,Q))%Q)%Q
        C = (((czen - cnum[i])*pow(3,hzen - hnum[i],Q))%Q + ((hzen - hnum[i])*pow(3,hzen - hnum[i]-1,Q))%Q)%Q
        K = (A*C)%Q
        ans = (ans + K)%Q
print(int(ans))
Problem p03326 (python)

Fixed submission:
P = [(1,1,1), (1,1,-1), (1,-1,1), (1,-1,-1), (-1,1,1), (-1,1,-1), (-1,-1,1), (-1,-1,-1)]
N, M = map( int, input().split())
X = [0]*N
Y = [0]*N
Z = [0]*N
ans = 0
for i in range(N):
    X[i], Y[i], Z[i] = map( int, input().split())
for i in range(8):
    a, b, c = P[i]
    W = [0]*N
    for j in range(N):
        W[j] = a*X[j] + b*Y[j] + c*Z[j]
    W.sort(key=None, reverse = True)
    ans = max( ans, sum(W[:M]))
print(ans)

Buggy submission (verdict: Runtime Error):
import itertools
n, m = map(int, input().split())
hyoka = [""] * n
for i in range(n):
    hyoka[i] = input().split()
for i in range(n):
    hyoka[i] = [int(s) for s in hyoka[i]]
hyou = [""]
for iroiro in list(itertools.combination(hyoka, m)):
    sum_1 = sum[beau[1] for beau in iroiro]
    sum_1 = abs(sum_1)
    sum_2 = sum[beau[2] for beau in iroiro]
    sum_2 = abs(sum_2)
    sum_3 = sum[beau[3] for beau in iroiro]
    sum_3 = abs(sum_3)
    som = sum_1 + sum_2 + sum_3
    hyou = hyou.append(som)
print(max(hyou))
Problem p03345 (python)

Fixed submission:
a, b, c, k = map( int, input().split())
if k%2 == 0:
    print(a-b)
else:
    print(b-a)

Buggy submission (verdict: Runtime Error):
N = int( input())
A = [ int( input()) for _ in range(N)]
now = 0
ans = 0
if A[0] != 0:
    ans = -1
else:
   for i in range(1,N):
       a = A[i]
       # if a > i:
       #     ans = -1
       #     break
       #el
       if now + 1 == a:
           ans += 1
           now += 1
       elif now >= a:
           ans += a
           now = a
       else:
           ans = -1
           break
print(ans)
Problem p03652 (python)

Fixed submission:
from collections import Counter
N, M = map( int, input().split())
A = [ [] for _ in range(N)]
for i in range(N):
    A[i] = list( map( int, input().split())) 
V = [1]*(M+1)
ans = N
for _ in range(M-1):
    W = []
    for i in range(N):
        while True:
            if V[A[i][0]] == 1:
                W.append(A[i][0])
                break
            else:
                A[i].pop(0)
    a, c = Counter(W).most_common()[0]
    ans = min( ans, c)
    V[a] = 0
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import Counter
N, M = map( int, input().split())
A = [ [] for _ in range(N)]
for i in range(N):
    A[i] = list( map( int, input().split())) 
V = [1]*(M+1)
ans = N
for _ in range(M-1):
    W = []
    for i in range(N):
        if A[i]:
            a = A[i].pop(0)
            if V[a] == 1:
                W.append(a)
    a, c = Counter(W).most_common()[0]
    ans = min( ans, c)
    V[a] = 0
print(ans)
Problem p03775 (python)

Fixed submission:
N = int( input())
ans = len( str(N))
for i in range(1,int( N**(1/2))+1):
    if N%i == 0:
        ans = min( ans, max( len( str(i)), len( str(N//i))))
print(ans)

Buggy submission (verdict: Runtime Error):
N = int( input())
ans = len( str(N))
for i in range(1,int( N**(1/2))+1):
    if N%i == 0:
        ans = min( ans, max( len(i), len(N//i)))
        break
print(ans)

Buggy submission (verdict: Runtime Error):
N = int( input())
ans = len( str(N))
for i in range(N//2,N):
    if N%i == 0:
        ans = min( ans, len( str(i)))
        break
print(ans)

Buggy submission (verdict: Runtime Error):
N = int( input())
ans = len(N)
for i in range(N//2,N):
    if N%i == 0:
        ans = min( ans, len(i))
        break
print(ans)
Problem p04021 (python)

Fixed submission:
import sys
from collections import defaultdict
input = sys.stdin.readline
def main():
    N = int( input())
    A = [ int( input()) for _ in range(N)]
    B = sorted(A)
    d = defaultdict( int)
    for i in range(N):
        d[B[i]] = i
    odd = 0
    for i in range(N):
        if (i+1)%2 == d[A[i]]%2 and d[A[i]]%2 == 1:
            odd += 1
    print(odd)
    
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#import sys
#from collections import defaultdict
input = sys.stdin.readline
def main():
    N = int( input())
    A = [ int( input()) for _ in range(N)]
    B = sorted(A)
    d = defaultdict( int)
    for i in range(N):
        d[B[i]] = i
    odd = 0
    for i in range(N):
        if (i+1)%2 == d[A[i]]%2 and d[A[i]]%2 == 1:
            odd += 1
    print(odd)
    
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#import sys
#from collections import defaultdict
input = sys.stdin.readline
def main():
    N = int( input())
    A = [ int( input()) for _ in range(N)]
    B = sorted(A)
    d = defaultdict( int)
    for i in range(N):
        d[B[i]] = i
    odd = 0
    for i in range(N):
        if (i+1)%2 == d[A[i]]%2 and d[A[i]]%2 == 1:
            odd += 1
    print(odd)
    
if __name__ == '__main__':
    main()
Problem p02571 (python)

Fixed submission:
import sys


def resolve(in_):
    S = next(in_).strip()
    T = next(in_).strip()

    ans = 0
    for i in range(len(S)):
        if len(S) - i < len(T):
            break
        v = 0
        for x, y in zip(S[i:], T):
            if x == y:
                v += 1
            ans = max(ans, v)
    
    return len(T) - ans
    

def main():
    answer = resolve(sys.stdin)
    print(answer)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys


def resolve(in_):
    S = next(in_).strip()
    T = next(in_).strip()

    x = 0
    for i in range(len(S)):
        if len(S) - i < len(T):
            break
        v = 0
        for x, y in zip(S[i:], T)
            if x == y:
                v += 1
            x = max(x, len(T) - v)
    
    return x
    

def main():
    answer = resolve(sys.stdin)
    print(answer)


if __name__ == '__main__':
    main()
Problem p02598 (python)

Fixed submission:
import sys
from math import ceil, floor

def cut_logs(x, a):
    return sum(ceil(ai / x) - 1 for ai in a)


def resolve(in_):
    n, k = map(int, next(in_).split())
    logs = tuple(map(int, next(in_).split()))  # A
    assert n == len(logs)
    max_logs = max(logs)

    lb = 0
    ub = max_logs
    while ub - lb > 1:
        mid = (ub + lb) // 2
        v = cut_logs(mid, logs)
        # print(f'{lb=} {mid=} {ub=} {v<=k=} {v=} {k=}')
        if v <= k:
            ub = mid
        else:
            lb = mid
    # print(f'end: {lb=} {mid=} {ub=} {v<=k=} {v=} {k=}')

    return ub


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys


def resolve(in_):
    n = int(next(in_))
    c = next(in_).strip()

    ans = 0
    left = 0
    right = len(c) - 1
    while left < right:
        if c[left] == 'R':
            left += 1
            continue
        if c[right] == 'W':
            right -= 1
            continue
        left += 1
        right -= 1
        ans += 1

    return ans


def main():
    answer = resolve(sys.stdin)
    print(answer)


if __name__ == '__main__':
    main()
Problem p02712 (python)

Fixed submission:
import sys
n = int(sys.stdin.read())
print(sum(i for i in range(1, n + 1) if i % 3 and i % 5))

Buggy submission (verdict: Runtime Error):
import sys
n = int(sys.stdin)
print(sum(i for i in range(1, n + 1) if i % 3 and i % 5))
Problem p02753 (python)

Fixed submission:
import sys
print('Yes' if len(set(sys.stdin.read().strip())) >= 2 else 'No')

Buggy submission (verdict: Runtime Error):
import sys
print('Yes' if set(sys.stdin.read().strip()) >= 2 else 'No')
Problem p02785 (python)

Fixed submission:
import sys

N, K = map(int, next(sys.stdin.buffer).split())
H = list(map(int, next(sys.stdin.buffer).split()))

if K:
  H.sort()
  print(sum(H[:-K]))
else:
  print(sum(H))

Buggy submission (verdict: Runtime Error):
import sys

N, K = map(int, next(sys.stdin.buffer).split())
H = sorted(map(int, next(sys.stdin.buffer).split()))

print(sum(H[:K])
Problem p02571 (python)

Fixed submission:
s = input()
t = input()
ans = 10**8
for i in range(len(s)-len(t)+1):
    cnt = 0
    for j in range(len(t)):
        if s[i+j] != t[j]: cnt += 1
    ans = min(ans, cnt)

print(ans)

Buggy submission (verdict: Runtime Error):
s = input()
t = input()
ans = 10**8
for i in range(len(s)):
    cnt = 0
    for j in range(len(t)):
        if s[i+j] != t[j]: cnt += 1
    ans = min(ans, cnt)

print(ans)
Problem p02735 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    H, W = map(int, rl().split())
    s = [list(input()) for _ in range(H)]
    
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = s[0][0] == '#'
    for i in range(1, W):
        dp[0][i] = dp[0][i - 1] + (s[0][i - 1] == '.' and s[0][i] == '#')
    for i in range(1, H):
        dp[i][0] = dp[i - 1][0] + (s[i - 1][0] == '.' and s[i][0] == '#')
    
    for i in range(1, H):
        for j in range(1, W):
            dy = s[i - 1][j] == '.' and s[i][j] == '#'
            dx = s[i][j - 1] == '.' and s[i][j] == '#'
            dp[i][j] = min(dp[i - 1][j] + dy, dp[i][j - 1] + dx)
    print(dp[-1][-1])


if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    H, W = map(int, rl().split())
    s = [list(input()) for _ in range(H)]
    
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = s[0][0] == '#'
    for i in range(1, W):
        dp[0][i] = dp[0][i - 1] + (s[0][i] == '#')
        dp[i][0] = dp[i - 1][0] + (s[i][0] == '#')
    
    for i in range(1, H):
        for j in range(1, W):
            dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + (s[i][j] == '#')
    print(dp[-1][-1])


if __name__ == '__main__':
    solve()
Problem p02777 (python)

Fixed submission:
def solve():
    S, T = map(str, input().split())
    A, B = map(int, input().split())
    U = input()
    if S == U:
        print(A - 1, B)
    else:
        print(A, B - 1)


if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
def solve():
    S, T, A, B, U = map(int, open(0).read().split())
    if S == U:
        print(A - 1, B)
    else:
        print(A, B - 1)


if __name__ == '__main__':
    solve()
Problem p02819 (python)

Fixed submission:
import math
from bisect import bisect_left


def eratosthenes(n):
    prime = []
    limit = math.sqrt(n)
    data = [i + 1 for i in range(1, n)]
    while True:
        p = data[0]
        if limit <= p:
            return prime + data
        prime.append(p)
        data = [e for e in data if e % p != 0]


def solve():
    X = int(input())
    primes = eratosthenes(100010)
    index = bisect_left(primes, X)
    print(primes[index])


if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import math
from bisect import bisect_left
from random import random


def eratosthenes(n):
    prime = []
    limit = math.sqrt(n)
    data = [i + 1 for i in range(1, n)]
    while True:
        p = data[0]
        if limit <= p:
            return prime + data
        prime.append(p)
        data = [e for e in data if e % p != 0]


def is_prime(q, k=50):
    q = abs(q)
    if q == 2: return True
    if q < 2 or q & 1 == 0: return False

    d = (q - 1) >> 1
    while d & 1 == 0:
        d >>= 1

    for i in range(k):
        a = random.randint(1, q - 1)
        t = d
        y = pow(a, t, q)
        while t != q - 1 and y != 1 and y != q - 1:
            y = pow(y, 2, q)
            t <<= 1
        if y != q - 1 and t & 1 == 0:
            return False
    return True


def solve():
    X = int(input())
    if is_prime(X):
        print(X)
    else:
        primes = eratosthenes(100010)
        index = bisect_left(primes, X)
        print(primes[index])


if __name__ == '__main__':
    solve()
Problem p02897 (python)

Fixed submission:
# coding: utf-8


def solve():
    N = int(input())
    count = 0
    
    for i in range(1, N+1):
        if i % 2 == 1:
            count += 1
    
    print(count/N)


if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
def solve():
    N, M = map(int, input().split())
    INF = 10 ** 9
    
    dp = [INF] * (2 ** N)
    dp[0] = 0
    for _ in range(M):
        a, b = map(int, input().split())
        c = list(map(lambda x: int(x) - 1, input().split()))
        open = 0
        for i in range(b):
            open += 1 << c[i]
        for i in range(2 ** N):
            if dp[i] + a <= dp[i | open]:
                dp[i | open] = dp[i] + a
    
    if dp[-1] == INF:
        print(-1)
    else:
        print(dp[-1])


if __name__ == '__main__':
    solve()
Problem p03013 (python)

Fixed submission:
def solve():
    N, M, *a = map(int, open(0).read().split())
    ok = [True for _ in range(N+1)]
    for i in range(M):
        ok[a[i]] = False
    MOD = 1000000007

    dp = [0 for _ in range(N+1)]
    dp[0] = 1
    for i in range(N):
        for j in range(i+1, min(N+1, i+3)):
            if ok[j]:
                dp[j] += dp[i]
                dp[j] %= MOD
    print(dp[N])


if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
def solve():
    N, M, *a = map(int, open(0).read().split())
    MOD = 1000000007

    dp = [0 for _ in range(N+1)]
    if 1 not in a:
        dp[1] = 1
        if 2 not in a:
            dp[2] = 2
    elif 2 not in a:
        dp[2] = 1

    for i in range(3, N+1):
        dp[i] = (dp[i-1] + dp[i-2]) % MOD
        if i in a:
            dp[i] = 0
    print(dp[-1])


if __name__ == '__main__':
    solve()
Problem p03041 (python)

Fixed submission:
def solve():
    N, K = map(int, input().split())
    s = input()
    s_lower = s.lower()
    s = list(s)
    s[K-1] = s_lower[K-1]
    print(''.join(s))

if __name__ == '__main__':
    solve()

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


def solve():
    N, M = map(int, input().split())
    xy_dict = defaultdict(int)
    xy = [[] for _ in range(N)]
    for _ in range(M):
        x, y, z = map(lambda j: int(j)-1, input().split())
        xy_dict[x] += 1
        xy_dict[y] += 1
        xy[x].append(y)
        xy[y].append(x)
    
    xy_sorted = sorted(xy_dict.items(), key=itemgetter(1), reverse=True)
    opened = [0 for _ in range(N)]
    ans = N
    length = len(xy_sorted)
    for i in range(length):
        parent = xy_sorted[i][0]
        if opened[parent] == 1:
            for child in xy[parent]:
                if opened[child] == 0:
                    opened[child] = 1
                    ans -= 1
            continue
        opened[parent] = 1
        for child in xy[parent]:
            opened[child] = 1
        ans -= xy_dict[parent]
    print(ans)


if __name__ == '__main__':
    solve()
Problem p03045 (python)

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


def solve():
    N, M = map(int, input().split())
    X = [0 for _ in range(M)]
    Y = X[:]
    for i in range(M):
        X[i], Y[i], _ = map(lambda x: int(x)-1, input().split())
    
    graph = csr_matrix(([1]*M, (X, Y)), (N, N))
    ans = connected_components(graph)[0]
    print(ans)


if __name__ == '__main__':
    solve()

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


def solve():
    N, M = map(int, input().split())
    xy_dict = defaultdict(int)
    xy = [[] for _ in range(N)]
    for _ in range(M):
        x, y, z = map(lambda j: int(j)-1, input().split())
        xy_dict[x] += 1
        xy_dict[y] += 1
        xy[x].append(y)
        xy[y].append(x)
    
    xy_sorted = sorted(xy_dict.items(), key=itemgetter(1), reverse=True)
    opened = [0 for _ in range(N)]
    ans = N
    for i in range(M):
        parent = xy_sorted[i][0]
        if opened[parent] == 1:
            for child in xy[parent]:
                if opened[child] == 0:
                    opened[child] = 1
                    ans -= 1
            continue
        opened[parent] = 1
        for child in xy[parent]:
            opened[child] = 1
        ans -= xy_dict[parent]
    print(ans)


if __name__ == '__main__':
    solve()

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


def solve():
    N, M = map(int, input().split())
    xy_dict = defaultdict(int)
    xy = [[] for _ in range(N+1)]
    for _ in range(M):
        x, y, z = map(int, input().split())
        xy_dict[x] += 1
        xy_dict[y] += 1
        xy[x].append(y)
        xy[y].append(x)
    
    xy_sorted = sorted(xy_dict.items(), key=itemgetter(1), reverse=True)
    opened = [0 for _ in range(N+1)]
    ans = N
    for i in range(M):
        parent = xy_sorted[i][0]
        if opened[parent] == 1:
            continue
        opened[parent] = 1
        for child in xy[parent]:
            opened[child] = 1
        ans -= xy_dict[parent]
    print(ans)


if __name__ == '__main__':
    solve()