Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import combinations

def main():
    n, s = sys.stdin.read().split()
    n = int(n)
    res = set()
    for comb in combinations(s, 3):
        res.add(comb)
    
    ans = len(res)
    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
from itertools import combinations, product
from string import digits
from collections import Counter

def main():
    n, s = sys.stdin.read().split()
    n = int(n)
    
    # t = [None] * (n+1)
    # c = Counter(s)
    # t[0] = set(s)
    # # for i in range(n):
    # #     c[s[i]] -= 1
    # #     if c[s[i]] == 0:
    # #         t[i+1] = t[i] - set([s[i]])
    # #     else:
    # #         t[i+1] = t[i]
    # # print(t)


    
    cand = list(''.join(p) for p in product(digits, repeat=3))
    # print(cand)
    # for comb in combinations(s, 3):
    #     res.add(comb)
    
    # ans = len(res)
    # print(ans)

    res = set()
    for c in cand:
        i = s.find(c[0])
        if i == -1:
            continue
        j = s.find(c[1], i+1)
        if j == -1:
            continue
        k = s.find(c[2], j+1)
        if k == -1:
            continue
        
        res.add(c)
        
    ans = len(res)
    print(ans)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import sys
def main():
    t1, t2, a1, a2, b1, b2 = map(int, sys.stdin.read().split())
    A1 = t1 * a1
    A2 = t2 * a2
    B1 = t1 * b1
    B2 = t2 * b2

    d = abs(A1 + A2 - B1 - B2)
    if A1 == B1:
        ans = 0
    elif A1 > B1:
        if A1 + A2 > B1 + B2:
            ans = 0
        elif A1 + A2 == B1 + B2:
            ans = 'infinity'
        else:
            (A1 - B1) // d * 2 + 1

    
    elif A1 < B1:
        if A1 + A2 < B1 + B2:
            ans = 0
        elif A1 + A2 == B1 + B2:
            ans = 'infinity'
        else:
            ans = (B1 - A1) // d * 2 + 1

    print(ans)
    
if __name__ == '__main__':
    main()

Fixed submission:
import sys
from math import ceil
def main():
    T1, T2, A1, A2, B1, B2 = map(int, sys.stdin.read().split())
    
    BA1 = A1 - B1
    BA2 = A2 - B2

    if BA1 == 0:
        print(0)
        sys.exit()

    # A1, B1の速い方が速い方をA1とする
    if BA1 < 0:
        BA1 = -BA1
        BA2 = -BA2
    
    d = BA1 * T1 + BA2 * T2
    if d > 0:
        print(0)
        sys.exit()
    if d == 0:
        print('infinity')
        sys.exit()
    
    n = ceil((- BA1 * T1) / d)
    if n * d == - BA1 * T1:
        ans = 2 * n
    else:
        ans = 2 * n - 1
    
    print(ans)
    
if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
# 2019-11-24 20:59:47(JST)
import sys
from string import ascii_uppercase as alphabet

# alpha_num = dict((alpha, num) for num, alpha in enumerate(alphabet, 0))
alpha_ind = dict((alphabet[i], i) for i in range(26))

def main():
    n, s = sys.stdin.read().split()
    n = int(n)

    t = ''
    for char in s:
        t += alphabet[(alpha_num[char] + n) % 26]
    
    print(t)

if __name__ == '__main__':
    main()


Fixed submission:
# 2019-11-24 20:59:47(JST)
import sys
# import numpy as np
from string import ascii_uppercase as alphabet

alphabet = alphabet[-1] + alphabet[0:]

alpha_num = enumerate(alphabet, 0)

alpha_num = dict((alpha, num) for num, alpha in alpha_num)

def main():
    n, s = sys.stdin.read().split()
    n = int(n)

    t = ''
    for c in s:
        t += alphabet[(alpha_num[c] + n) % 26]
    
    print(t)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import sys

def main():
    n, *a = map(int, sys.stdin.readline().split())

    l = 0
    r = sum(a)
    m = sum(a)
    for i in range(n):
        l += a[i]
        r -= a[i]
        m = min(m, abs(l - r))
    
    print(m)


if __name__ == '__main__':
    main()

Fixed submission:
# 2019-11-23 21:00:35(JST)
import sys
import itertools

def main():
    n, *A = map(int, sys.stdin.read().split())

    S = sum(A)
    l = 0
    for i in range(n):
        l += A[i]
        if l >= S // 2:
            if l == S / 2:
                ans = 0
                break
            else:
                if l == S // 2 - 1:
                    ans = 1
                    break
                else:
                    if l - A[i] >= S - l:
                        ans = A[i] + (S - l) - (l - A[i])
                    else:
                        ans = l - (S - l)
                    break

    print(ans)





if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
# 2019-11-16 21:01:15(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
from scipy.misc import comb # float
# import numpy as np 


mod = 10 ** 9 + 7

def main():
    x, y = [int(x) for x in sys.stdin.readline().split()]
    # n(+1, +2), m(+2, +1)
    n, m = (2 * y - x) / 3, (2 * x - y) / 3

    if n != abs(int(n)) or m != abs(int(m)):
        print(0)
        sys.exit()
    else:
        n, m = int(n), int(m)
        ans = comb(n+m, n, exact=True) % mod
        print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
# 2019-11-16 21:01:15(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
from functools import reduce
import operator as op
# from scipy.misc import comb # float
# import numpy as np 


mod = 10 ** 9 + 7

def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])
    
    return result


def main():
    x, y = [int(x) for x in sys.stdin.readline().split()]
    # n(+1, +2), m(+2, +1)
    n, m = (2 * y - x) / 3, (2 * x - y) / 3

    if n != abs(int(n)) or m != abs(int(m)):
        print(0)
        sys.exit()
    else:
        n, m = int(n), int(m)
        ans = comb(n+m, n) % mod
        print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
import sys
s = [s for s in sys.stdin.readline().rstrip()]

ans = 0
if s[0] == '>':
    count = 1
    i = 1
    while s[i] == '>':
        count += 1
        i += 1
    del s[:i]
    ans += (1 + count) * count // 2

if s[-1] == '<':
    count = 1
    i = -2
    while s[i] == '>':
        count += 1
        i -= 1
    del s[i+1:]
    ans += count
    (1 + count) * count // 2

s.append(None)

while s != [None]:
    l_c, r_c = 0, 0
    i = 0
    # if s[i] == '<':
    while s[i] == '<':
        l_c += 1
        i += 1
    del s[:i]

    j = 0
    # if s[j] == '>':
    while s[j] == '>':
        r_c += 1
        j += 1
    del s[:j]

    if l_c >= r_c:
        ans += (1 + l_c) * l_c // 2 + r_c * (r_c - 1) // 2
    else:
        ans += l_c * (l_c - 1) // 2 + (1 + r_c) * r_c // 2

print(ans)

Fixed submission:
import sys
from collections import deque
s = deque(s for s in sys.stdin.readline().rstrip())

s.append(None)
ans = 0
while s[0] != None:
    l_c = 0
    while s[0] == '<':
        l_c += 1
        s.popleft()
    
    r_c = 0
    while s[0] == '>':
        r_c += 1
        s.popleft()

    if l_c >= r_c:
        ans += (1 + l_c) * l_c // 2 + r_c * (r_c - 1) // 2
    else:
        ans += l_c * (l_c - 1) // 2 + (1 + r_c) * r_c // 2

print(ans)
Buggy python submission (verdict: Runtime Error):
a, b = map(int, input())
if a <= 9 and b <= 9:
    ans = a * b
else:
    ans = -1

print(ans)
    

Fixed submission:
a, b = map(int, input().split())
if a <= 9 and b <= 9:
    ans = a * b
else:
    ans = -1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

for x in range(1, 10**6+1):
    if x**2 >= n:
        y = x
        break

i, j = y, y
while True:
    if i*j == n:
        ans = i + j - 2
        break
    elif i*j > n:
        i -= 1
    elif i*j < n:
        j += 1

print(ans)

Fixed submission:
import math

n = int(input())

ans  = 10 ** 12 
for i in range(1, math.floor(math.sqrt(n)) + 1):
    if n%i != 0:
        continue
    j = int(n/i)
    ans = min(ans, i+j-2)

print(ans)

Buggy python submission (verdict: Runtime Error):
import math

n = int(input())

ans  = 10 ** 7 
for i in range(1, math.sqrt(n) + 1):
    if n%i != 0:
        continue
    j = n/i
    ans = min(ans, i+j-2)

print(ans)

Fixed submission:
import math

n = int(input())

ans  = 10 ** 12 
for i in range(1, math.floor(math.sqrt(n)) + 1):
    if n%i != 0:
        continue
    j = int(n/i)
    ans = min(ans, i+j-2)

print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())

for x in range(1, 10**6+1):
    if x**2 >= n:
        y = x
        break

i, j = y, y
for x in range(10**5):
    if i*j == n:
        ans = i + j - 2
        break
    elif i*j > n:
        i -= 1
    elif i*j < n:
        j += 1

print(ans)

Fixed submission:
import math

n = int(input())

ans  = 10 ** 12 
for i in range(1, math.floor(math.sqrt(n)) + 1):
    if n%i != 0:
        continue
    j = int(n/i)
    ans = min(ans, i+j-2)

print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())

for x in range(1, 10**6):
    if x**2 >= n:
        y = x
        break

i, j = y, y
for x in range(10**6):
    if i*j == n:
        ans = i + j - 2
        break
    elif i*j > n:
        i -= 1
    elif i*j < n:
        j += 1

print(ans)

Fixed submission:
import math

n = int(input())

ans  = 10 ** 12 
for i in range(1, math.floor(math.sqrt(n)) + 1):
    if n%i != 0:
        continue
    j = int(n/i)
    ans = min(ans, i+j-2)

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

horizontal_length = b * 2

uncovered_part = a - horizontal_length

if uncovered_part < 0:
  uncovered_part = 0
  
print str(uncovered_part)
  

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

horizontal_length = b * 2

uncovered_part = a - horizontal_length

if uncovered_part < 0:
  uncovered_part = 0
  
print(str(uncovered_part))

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

horizontal_length = b * 2

uncovered_part = a - horizontal_length

if uncovered_part > 0:
  return
else:
  uncovered_part = 0
  
print str(uncovered_part)
  

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

horizontal_length = b * 2

uncovered_part = a - horizontal_length

if uncovered_part < 0:
  uncovered_part = 0
  
print(str(uncovered_part))

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

horizontal_length = b * 2

uncovered_part = a - horizontal_length

if uncovered_part > 0:
  return
else:
  uncovered_part = 0
  
print uncovered_part
  

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

horizontal_length = b * 2

uncovered_part = a - horizontal_length

if uncovered_part < 0:
  uncovered_part = 0
  
print(str(uncovered_part))
Buggy python submission (verdict: Runtime Error):
# 2019-11-25 06:26:24(JST)
import sys

def main():
    s, k = sys.stdin.read().split()
    k = int(k)

    count = 0
    same = 1
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            same += 1
        else:
            if count == 0:
                first_same = same
            count += same // 2
            same = 1
    
    bl = False
    if s[-1] == s[0]:
        if same % 2 == 1 and first_same % 2 == 1:
            count += same // 2 + 1
            bl = True
        else:
            count += same // 2
    else:
        count += same // 2

    ans = count * k
    if bl:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
# 2019-11-25 06:26:24(JST)
import sys

def main():
    s, k = sys.stdin.read().split()
    k = int(k)

    if len(s) == 1:
        print(k // 2)
        sys.exit()
    
    if s.count(s[0]) == len(s):
        if len(s) % 2 == 0:
            ans = len(s) // 2 * k
        else:
            # if k % 2 == 0:
            ans = len(s) * k // 2
            # else:
            #     ans = len(s) // 2 * (k // 2 + 1) + (len(s) // 2 + 1) * k // 2
        print(ans)
        sys.exit()

    count = 0
    same = 1
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            same += 1
        else:
            if count == 0:
                first_same = same
            count += same // 2
            same = 1
    
    bl = False
    if s[-1] == s[0]:
        if same % 2 == 1 and first_same % 2 == 1:
            count += same // 2 + 1
            bl = True
        else:
            count += same // 2
    else:
        count += same // 2

    ans = count * k
    if bl:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
inf = float('inf')

n, m = map(int, sys.stdin.readline().split())
graph = [None] * m
for i in range(m):
  a, b = map(int, sys.stdin.readline().split())
  res = 0
  for c in map(int, sys.stdin.readline().split()):
    res |= 1 << (c - 1)
    graph[i] = (a, res)


def main():
  dp = [[0] * (1 << n) for _ in range(m + 1)]
  for i in range(1, 1 << n):
    dp[0][i] = inf

  for i in range(m):
    for j in range(1 << n):
      dp[i+1][j] = min(dp[i][j ^ (j & graph[i][1])] + graph[i][0], dp[i][j])

  ans = dp[m][(1<<n)-1]
  print(-1 if ans == inf else ans)

if __name__ == '__main__':
  main()

Fixed submission:
import sys
inf = float('inf')

n, m = map(int, sys.stdin.readline().split())
graph = [None] * m
for i in range(m):
  a, b = map(int, sys.stdin.readline().split())
  res = 0
  for c in map(int, sys.stdin.readline().split()):
    res |= 1 << (c - 1)
    graph[i] = (a, res)


def main():
  dp = [[0] * (1 << n) for _ in range(m + 1)]
  for i in range(1, 1 << n):
    dp[0][i] = inf

  for i in range(m):
    for j in range(1 << n):
      dp[i+1][j] = min(dp[i][j ^ (j & graph[i][1])] + graph[i][0], dp[i][j])

  ans = dp[m][(1<<n)-1]
  print(-1 if ans == inf else ans)

if __name__ == '__main__':
  main()
Buggy python submission (verdict: Time Limit Exceeded):
# 2019-11-18 17:34:23(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect_left as bi_l, bisect_right as bi_r, insort_left as in_l
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


def main():
    n, m, *a = [int(x) for x in sys.stdin.read().split()]
    a.sort()

    for _ in range(m):
        highest = a.pop()
        in_l(a, highest // 2)
    
    print(sum(a))
    
if __name__ == "__main__":
    main()


Fixed submission:
# 2019-11-18 17:34:23(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# import re
import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


def main():
    n, m = [int(x) for x in sys.stdin.readline().split()]
    a = [-int(x) for x in sys.stdin.readline().split()]

    heapq.heapify(a)
    for _ in range(m):
        highest = - heapq.heappop(a)
        heapq.heappush(a, -(highest // 2))
    
    print(-sum(a))
    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# 2019-11-18 17:34:23(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


def main():
    n, m, *a = [int(x) for x in sys.stdin.read().split()]

    for _ in range(m):
        a.sort()
        a[-1] //= 2
    
    print(sum(a))
        


if __name__ == "__main__":
    main()


Fixed submission:
# 2019-11-18 17:34:23(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# import re
import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


def main():
    n, m = [int(x) for x in sys.stdin.readline().split()]
    a = [-int(x) for x in sys.stdin.readline().split()]

    heapq.heapify(a)
    for _ in range(m):
        highest = - heapq.heappop(a)
        heapq.heappush(a, -(highest // 2))
    
    print(-sum(a))
    
if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
# 2019-11-16 10:55:08(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 
import re

def main():
    # 左がRで右がLとなっている箇所でそれぞれどちらかにあつまる。10^100 は実質無限なので距離が奇数か偶数かで場合わけ
    s = sys.stdin.readline().rstrip() + 'R' # 後々のループ処理のときのことを考えて最後にRを足しておく
    n = len(s) - 1
    all_occurrences_of_rl = [m.start() for m in re.finditer('RL', s)]
    
    counts = [0 for _ in range(n)]
    left = 0
    for i in range(len(all_occurrences_of_rl)):
        right = all_occurrences_of_rl[i] + 1
        while s[right+1] == 'L':　# 次がLだったら右端を拡張していく
            right += 1
        
        r_count = 1 + (all_occurrences_of_rl[i] - left) // 2 + (right - all_occurrences_of_rl[i]) // 2
        l_count = (right - left + 1) - r_count
        counts[all_occurrences_of_rl[i]] = r_count
        counts[all_occurrences_of_rl[i] + 1] = l_count

        left = right + 1
    
    for i in range(n):
        print(counts[i], end=' ')
        

if __name__ == "__main__":
    main()


Fixed submission:
# 2019-11-16 10:55:08(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 
import re

def main():
    # 左がRで右がLとなっている箇所でそれぞれどちらかにあつまる。10^100 は実質無限なので距離が奇数か偶数かで場合わけ
    s = sys.stdin.readline().rstrip() + 'R' # 後々のループ処理のときのことを考えて最後にRを足しておく
    n = len(s) - 1
    all_occurrences_of_rl = [m.start() for m in re.finditer('RL', s)]
    
    counts = [0 for _ in range(n)]
    left = 0
    for i in range(len(all_occurrences_of_rl)):
        right = all_occurrences_of_rl[i] + 1
        while s[right+1] == 'L': # 次がLだったら右端を拡張していく
            right += 1
        
        r_count = 1 + (all_occurrences_of_rl[i] - left) // 2 + (right - all_occurrences_of_rl[i]) // 2
        l_count = (right - left + 1) - r_count
        counts[all_occurrences_of_rl[i]] = r_count
        counts[all_occurrences_of_rl[i] + 1] = l_count

        left = right + 1
    
    for i in range(n):
        print(counts[i], end=' ')
        

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
# 2019-11-13 08:30:54(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, m = [int(x) for x in sys.stdin.readline().split()]

    l, r = [], []
    for _ in range(m):
        li, ri = [int(x) for x in sys.stdin.readline().split()]
        l.append(li)
        r.append(ri)
    
    all_mighty_cards = set(range(1,n+1))
    for i in range(m):
        all_mighty_cards &= set(l[i:] + r[:i+1])
    


    print(max(all_mighty_cards) - min(all_mighty_cards) + 1 if all_mighty_cards else 0)

    

if __name__ == "__main__":
    main()


Fixed submission:
# 2019-11-13 08:30:54(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, m = [int(x) for x in sys.stdin.readline().split()]

    l, r = [], []
    for _ in range(m):
        li, ri = [int(x) for x in sys.stdin.readline().split()]
        l.append(li)
        r.append(ri)
    
    print(min(r) - max(l) + 1 if min(r) >= max(l) else 0)



if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys

n, m = map(int, sys.stdin.readline().split())
graph = [[] for _ in range(n+1)]
for _ in range(m):
    x, y, z = map(int, sys.stdin.readline().split())
    graph[x].append(y)
    graph[y].append(x)

def main():
    to_check = set(range(1, n+1))
    checked = set()
    cnt = 0
    while to_check:
        stack = [to_check.pop()]
        cnt += 1
        while stack:
            v = stack.pop()
            checked.add(v)
            to_check -= set([v])
            for u in graph[v]:
                if not u in checked:
                    stack.append(u)
    return cnt

if __name__ == '__main__':
    ans = main()
    print(ans)

Fixed submission:
import sys

n, m, *xyz = map(int, sys.stdin.read().split())
xyz = zip(*[iter(xyz)] * 3)

root = list(range(n+1))
height = [0] * (n + 1)

sys.setrecursionlimit(10**7)
def find_root(v):
    u = root[v]
    if u == v:
        return u
    w = find_root(u)
    root[v] = w
    return w

def unite(u, v):
    ru = find_root(u)
    rv = find_root(v)
    if ru == rv:
        return
    if height[u] >= height[v]:
        root[rv] = ru
        height[ru] = max(height[ru], height[rv] + 1)
    else:
        root[ru] = rv

def main():
    for x, y, z in xyz:
        unite(x, y)
    
    for i in range(1, n+1):
        find_root(i)

    return len(set(root[1:]))

if __name__ == '__main__':
    ans = main()
    print(ans)

Buggy python submission (verdict: Runtime Error):
import sys

n, m, *xyz = map(int, sys.stdin.read().split())
xyz = zip(*[iter(xyz)] * 3)

root = list(range(n+1))
height = [0] * (n + 1)

def find_root(v):
    u = root[v]
    if u == v:
        return u
    w = find_root(u)
    root[v] = w
    return w

def unite(u, v):
    ru = find_root(u)
    rv = find_root(v)
    if ru == rv:
        return
    if height[u] >= height[v]:
        root[rv] = ru
        height[ru] = max(height[ru], height[rv] + 1)
    else:
        root[ru] = rv

def main():
    for x, y, z in xyz:
        unite(x, y)
    
    for i in range(1, n+1):
        find_root(i)

    return len(set(root[1:]))

if __name__ == '__main__':
    ans = main()
    print(ans)

Fixed submission:
import sys

n, m, *xyz = map(int, sys.stdin.read().split())
xyz = zip(*[iter(xyz)] * 3)

root = list(range(n+1))
height = [0] * (n + 1)

sys.setrecursionlimit(10**7)
def find_root(v):
    u = root[v]
    if u == v:
        return u
    w = find_root(u)
    root[v] = w
    return w

def unite(u, v):
    ru = find_root(u)
    rv = find_root(v)
    if ru == rv:
        return
    if height[u] >= height[v]:
        root[rv] = ru
        height[ru] = max(height[ru], height[rv] + 1)
    else:
        root[ru] = rv

def main():
    for x, y, z in xyz:
        unite(x, y)
    
    for i in range(1, n+1):
        find_root(i)

    return len(set(root[1:]))

if __name__ == '__main__':
    ans = main()
    print(ans)