Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03354 (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())

N, M = map(int, input().split())
P = list(map(int, input().split()))

uf = UnionFind(N)

for _ in range(M):
  x, y = map(lambda x: int(x)-1, input().split())
  uf.union(x, y)

ans = 0

for i, p in enumerate(P):
  if uf.same(i, p-1):
    ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
class UnionFind():
  from collections import defaultdict
  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):
    ret = defaultdict(list)
    for i in range(self.n):
      ret[self.find(i)].append(i)
    return ret

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

N, M = map(int, input().split())
P = list(map(int, input().split()))

uf = UnionFind(N)

for _ in range(M):
  x, y = map(lambda x: int(x)-1, input().split())
  uf.union(x, y)

D = uf.all_group_members()

ans = 0

for L in D.values():
  check = set(L)
  for i in L:
    if P[i]-1 in check:
      ans += 1 

print(ans)
Problem p03399 (python)

Fixed submission:
ans = []

for _ in range(4):
  ans.append(int(input()))

print(min(ans[:2]) + min(ans[2:]))

Buggy submission (verdict: Runtime Error):
A, B, C, D = map(int, input().split())

print(min(A, B) + min(C, D))
Problem p03623 (python)

Fixed submission:
X, A, B = map(int, input().split())
D = {abs(X-A):"A", abs(X-B):"B"}

print(D[min(abs(X-A), abs(B-X))])

Buggy submission (verdict: Runtime Error):
X, A, B = map(int, input().split())

print(min(abs(X-A), abs(B-X))
Problem p02678 (python)

Fixed submission:
from collections import deque
n, m = [int(i) for i in input().split()]
rooms = [[] for i in range(n)]
dist = [-1] * n
for i in range(m):
    a, b = [int(i) for i in input().split()]
    rooms[a-1].append(b)
    rooms[b-1].append(a)

que = deque()
que.append(1)
dist[0] = 0
ans = [0] * n
while(len(que) != 0):
    v = que.popleft()
    for i in rooms[v-1]:
        if dist[i-1] == -1:
            dist[i-1] = dist[v-1] + 1
            que.append(i)
            ans[i-1] = v
print("Yes")
for i in ans[1:]:
    print(i)

Buggy submission (verdict: Runtime Error):
from collections import deque
n, m = [int(i) for i in input().split()]
rutes = [[]] * n
visited = [0] + [-1] * (n-1)
q = deque([0])

for i in range(n):
    a, b = [int(i) for i in input().split()]
    rutes[a-1].append(b)
    rutes[b-1].append(a)

cnt = 0
def visit(x):
    for i in rutes[x-1]:
        if visited[i] == -1:
            q.append(i)
            visited[i-1] = visited[x-1] + 1

while len(q) > 0:
    tmp = q.pop()
    visit(tmp)

for i in range(2,n+1):
    print(visited[i-1])
Problem p02705 (python)

Fixed submission:
from math import pi
r = int(input())
print(2 * pi * r)

Buggy submission (verdict: Runtime Error):
k
Problem p02714 (python)

Fixed submission:
n, s = int(input()), input()
a = ["R", "G", "B"]
cnt = [0, 0, 0]
for i in s:
    cnt[a.index(i)] += 1
c = cnt[0] * cnt[1] * cnt[2]
for i in range(1, n-1):
    for j in range(i+1, n):
        k = 2 * j - i
        if j-1 < k-1 < n:
            if s[i-1] != s[j-1] and s[j-1] != s[k-1] and s[k-1] != s[i-1]:
                c -= 1

print(c)

Buggy submission (verdict: Runtime Error):
@njit
def solve():
    n, s = int(input()), input()
    a = ["R", "G", "B"]
    cnt = [0, 0, 0]
        for i in s:
        cnt[a.index(i)] += 1
    c = cnt[0] * cnt[1] * cnt[2]
    for i in range(1, n-1):
        for j in range(i+1, n):
            k = 2 * j - i
            if j-1 < k-1 < n:
                if s[i-1] != s[j-1] and s[j-1] != s[k-1] and s[k-1] != s[i-1]:
                    c -= 1
    return c
print(solve())

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

cnt = 0
for i in range(1, n-2):
    for i in range(i+1, n-1):
        for k in range(j+1,n)
        if k != j * 2 - i:
            if s[i-1] != s[j-1] and s[i-1] != s[k-1] and s[j-1] != s[k-1]:
                cnt += 1

print(cnt)
Problem p02718 (python)

Fixed submission:
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

ok_count = 0
for i in a:
    if i >= sum(a) / (4 * m): ok_count += 1

print("Yes" if m <= ok_count else "No")

Buggy submission (verdict: Runtime Error):
n, m = [for i in input().split()]
a = [int(i) for i in input().split()]

ok_count = 0
for i in a:
    if i > 1 / (4 * sum(a)): ok_count += 1

print("Yes" if m >= ok_count else "No")
Problem p02719 (python)

Fixed submission:
n, k = [int(i) for i in input().split()]
print(min(n % k, k - n % k))

Buggy submission (verdict: Runtime Error):
n, k = [int(i) for i in input().split()]

if n > k:
    tmp = k 
    while not (n - tmp) % k == 0:
        tmp -= 1
    n = tmp
    
while tmp >= n:
    tmp = n
    n = abs(n - k)
    if tmp < n:
        break
print(tmp)

Buggy submission (verdict: Runtime Error):
n, k = [int(i) for i in input().split()]

if n > k:
    tmp = k 
    while not (n - tmp) % k == 0:
        tmp -= 1
    n = tmp
    
while tmp >= n:
    tmp = n
    n = abs(n - k)
    if tmp < n:
        break
print(tmp)

Buggy submission (verdict: Runtime Error):
n, k = [int(i) for i in input().split()]

if n > k:
    tmp = k 
    while not (n - tmp) % k == 0:
        tmp -= 1
    n = tmp
    
while tmp >= n:
    tmp = n
    n = abs(n - k)
    if tmp < n:
        break
print(tmp)

Buggy submission (verdict: Runtime Error):
n, k = [int(i) for i in input().split()]

if n > k:
    tmp = k 
    while not (n - tmp) % k == 0: tmp -= 1
    n = tmp
    
while tmp >= n:
    tmp = n
    n = abs(n - k)
    if tmp < n:
        break
print(tmp)
Problem p02772 (python)

Fixed submission:
n = int(input())
a = [int(i) for i in input().split() if int(i) % 2 == 0]
for i in a:
    if i % 3 != 0 and i % 5 != 0:
            print("DENIED")
            exit()
print("APPROVED")

Buggy submission (verdict: Runtime Error):
n = int(input())
a = [int(i) for i in input().split() if i % 2 == 0]
for i in a:
    if a % 3 != 0 and a % 5 != 0:
            print("DENIED")
            exit()
print("APPROVED")
Problem p02780 (python)

Fixed submission:
from itertools import accumulate

n, k = [int(i) for i in input().split()]
p = [int(i) for i in input().split()]
c = [0] + list(accumulate(p))
mx = 0
mx_n = 0
for i in range(len(c)-k):
    if mx < c[i+k] - c[i]:
        mx = c[i+k] - c[i]
        mx_n = i
ans = 0
for i in range(k):
    ans += (p[mx_n + i] + 1) / 2

print(ans)

Buggy submission (verdict: Runtime Error):
n, k = [int(i) for i in input().split()]
p = [int(i) for i in input().split()]
q = [0] * (n + 1)
for i in range(n):
    q[i+1] = q[i] + p[i]
mx = 0
mx_num = 0
for i in range(n-2):
    if mx < q[i+3] - q[i]:
        mx_num = i

ans = 0
for i in range(3):
    t = p[mx_num + i]
    ans += (t + 1) / 2

print(ans)
Problem p02786 (python)

Fixed submission:
def a(x):
    if x == 1: return 1
    if x > 1: return a(x // 2) * 2 + 1

print(a(int(input())))

Buggy submission (verdict: Runtime Error):
def a(x):
    if x == 1: return 1
    if x > 1: return a((a(x // 2)) * 2 + 1

print(a(int(input())))

Buggy submission (verdict: Runtime Error):
def a(x):
    if x == 1: return 1
    if x > 1: return a((x + 1) // 2)) * 2 + 1

print(a(int(input())))

Buggy submission (verdict: Runtime Error):
def a(x):
    if x == 1: return 1
    if x > 1: return a(x/2) * 2 + 1

print(a(int(input())))

Buggy submission (verdict: Runtime Error):
print(a([int(input())]))

def a(x):
    if x == 1: return 1
    if x > 1: return a(x/2) * 2 + 1
Problem p02802 (python)

Fixed submission:
n, m = [int(i) for i in input().split()]
ac = [0] * n
wa = [0] * n

for i in range(m):
    p, s = input().split()
    p = int(p)
    if ac[p-1] == 0:
        if s == "AC":
            ac[p-1] = 1
        elif s == "WA":
            wa[p-1] += 1

wa_sum = 0
for i in range(n):
    if ac[i] >= 1: wa_sum += wa[i]

print(sum(ac), wa_sum)

Buggy submission (verdict: Runtime Error):
n, m = [int(i) for i in input().split()]
ac = [0] * n
wa = [0] * n

for i in range(m):
    p, s = input().split()
    p = int(p)
    if ac[p-1] == 0:
        if s == "AC":
            ac[p-1] = 1
        elif s == "WA":
            wa[p-1] += 1

wa_sum = 0
for i in range(n):
    if ac[i] >= 1: wa_sum += wa[i]

print(sum(ac), sum_wa)
Problem p02830 (python)

Fixed submission:
n = int(input())
s, t = input().split()
out = ""
for i in range(n):
    out += s[i] + t[i]
print(out)

Buggy submission (verdict: Runtime Error):
n = int(input())
s, t = [int(input()) for i in range(2)]
out = ""
for i in range(n):
    out += s[i] + t[i]
print(out)
Problem p02899 (python)

Fixed submission:
n = int(input())
a = [int(i) for i in input().split()]
out = [0] * n
for i in range(len(a)): out[a[i] - 1] = i + 1
print(' '.join([str(i) for i in out]))

Buggy submission (verdict: Runtime Error):
n = int(input())
a = [int(i) for i in input().split()]
out = [] * n
for i in range(n):
    out[i] = (str(a.index(n-i)+1))
print(' '.join(out[::-1]))
Problem p02909 (python)

Fixed submission:
s = ['Sunny', 'Cloudy', 'Rainy']
print(s[(s.index(input())+1) % 3])

Buggy submission (verdict: Runtime Error):
s = ['Sunny', 'Cloudy', 'Rainy']
print(s[s.index(input()) + 1 % 3])

Buggy submission (verdict: Runtime Error):
s = ['Sunny', 'Cloudy', 'Rainy']
print(s[index(input())+1 % 3])
Problem p02918 (python)

Fixed submission:
n, k = [int(i) for i in input().split()]
s = input()

status = s[0]
cnt = 1
sa = []
for i in range(1, n):
    if s[i] != status:
        sa.append(status * cnt)
        cnt = 0
        status = s[i]
    cnt += 1
sa.append(status * cnt)

for i in range(1, min(k*2, len(sa)), 2):
    sa[i] = ("R" if sa[i][0] == "L" else "L") * len(sa[i])

sa = ''.join(sa)

ans = 0
for i in range(n):
    if sa[i] == "L":
        if i > 0: 
            if sa[i-1] == "L": ans += 1
    else:
        if i < n-1:
            if sa[i+1] == "R": ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
import itertools
n, k = [int(i) for i in input().split()]
s = input()

status = s[0]
cnt = 1
sa = []
for i in range(1, n):
    if s[i] != status:
        sa.append(status * cnt)
        cnt = 0
        status = s[i]
    cnt += 1
sa.append(status * cnt)

for i in range(1, min(k*2, n), 2):
    sa[i] = ("R" if sa[i][0] == "L" else "L") * len(sa[i])

sa = ''.join(sa)

ans = 0
for i in range(n):
    if sa[i] == "L":
        if i > 0: 
            if sa[i-1] == "L": ans += 1
    else:
        if i < n-1:
            if sa[i+1] == "R": ans += 1

print(ans)
Problem p02971 (python)

Fixed submission:
n = int(input())
a = [int(input()) for i in range(n)]
mx = max(a)
mxi = a.index(mx)
mx2 = max(a[:mxi] + a[mxi+1:])

for i in range(n):
    if i == mxi: print(mx2) 
    else: print(mx)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = [int(input()) for i in range(n)]
mx = max(a)
mxi = a.index(mx)
mx2 = a[:mxi] + a[mxi+1:]

for i in range(n):
    if i == mxi: print(mx2) 
    else: print(mn)
Problem p02995 (python)

Fixed submission:
import fractions
a, b, c, d = [int(i) for i in input().split()]
"""
print(b // c, b // d, b // ((c * d) // fractions.gcd(c,d)), (a-1) // c, (a-1) // d, (a-1) // ((c * d) // fractions.gcd(c,d)))
print((c * d) // fractions.gcd(c,d))
"""
print(
    b - a + 1 - 
    (b // c + b // d 
        - b // ((c * d) // fractions.gcd(c,d)) 
    - (a-1) // c - (a-1) // d 
        + (a-1) // ((c * d) // fractions.gcd(c,d)))
)

Buggy submission (verdict: Runtime Error):
import math
a, b, c, d = [int(i) for i in input().split()]
"""
print(b // c, b // d, b // ((c * d) // math.gcd(c,d)), (a-1) // c, (a-1) // d, (a-1) // ((c * d) // math.gcd(c,d)))
print((c * d) // math.gcd(c,d))
"""
print(
    b - a + 1 - 
    (b // c + b // d 
        - b // ((c * d) // math.gcd(c,d)) 
    - (a-1) // c - (a-1) // d 
        + (a-1) // ((c * d) // math.gcd(c,d)))
)
Problem p03001 (python)

Fixed submission:
w, h, x, y = [int(i) for i in input().split()]
print(w * h / 2, int(x * 2 == w and y * 2 == h))

Buggy submission (verdict: Runtime Error):
w, h, x, y = [int(i) for i in input().split()]
print(max(w * x, h * y), int(w * x == h * y)
Problem p03080 (python)

Fixed submission:
n = int(input())
a = input()
cnt = 0
for i in a:
  cnt += (i == "R")
print("YNeos"[(cnt <= n // 2)::2])

Buggy submission (verdict: Runtime Error):
N = int(input())
a = 0
for i in input():
    if(i == R):
        a += 1
print("Yes" if N/2 < a else "No")
Problem p03103 (python)

Fixed submission:
n, m = [int(i) for i in input().split()]
ab = [[int(i) for i in input().split()] for j in range(n)]
ab = sorted(ab, key=lambda x:x[0])

sm = 0
i = 0
for i in ab:
    if m > i[1]:
        sm += i[0] * i[1]
        m -= i[1]
    else:
        sm += i[0] * m
        break
print(sm)

Buggy submission (verdict: Runtime Error):
n, m = [int(i) for i in input().split()]
d = sorted({i[0]: i[1] for i in [[int(j) for j in input().split()] for k in range(n)]}.items(), key = lambda x:x[0])
sm = 0
i = 0
while True:
    if m > d[i][1]:
        sm += d[i][0] * d[i][1]
        m -= d[i][1]
    else:
        sm += d[i][0] * m
        break
    i += 1
print(sm)

Buggy submission (verdict: Runtime Error):
n, m = [int(i) for i in input().split()]
d = sorted({i[0]: i[1] for i in [[int(j) for j in input().split()] for k in range(n)]}.items(), key = lambda x:x[0])
sm = 0
i = 0
while True:
    if m >= d[i][1]:
        sm += d[i][0] * d[i][1]
        m -= d[i][1]
    else:
        sm += d[i][0] * m
        break
    i += 1
print(sm)

Buggy submission (verdict: Runtime Error):
n, m = [int(i) for i in input().split()]
d = sorted({i[0]: i[1] for i in [[int(j) for j in input().split()] for k in range(n)]}.items(), key = lambda x:x[0])
sm = 0
i = 0
while True:
    if m >= d[i][1]:
        sm += d[i][0] * d[i][1]
        m -= d[i][1]
    else:
        sm += d[i][0] * m
        break
    i += 1
print(sm)
Problem p03262 (python)

Fixed submission:
from fractions import gcd
n, X = [int(i) for i in input().split()]
x = [abs(int(i) - X) for i in input().split()]
ans = 0
for i in x:
    ans = gcd(ans, i)
print(ans)

Buggy submission (verdict: Runtime Error):
from math import gcd
n, X = [int(i) for i in input().split()]
x = [abs(int(i) - X) for i in input().split()]
ans = 0
for i in x:
    ans = gcd(ans, i)
print(ans)
Problem p03280 (python)

Fixed submission:
a,b=[int(i) for i in input().split()]
print((a-1)*(b-1))

Buggy submission (verdict: Runtime Error):
a,b=[int(i) for i in input.split()]
print((a-1)*(b-1))

Buggy submission (verdict: Runtime Error):
a,b=[int(i) for i in input(split())]
print((a-1)*(b-1))
Problem p03544 (python)

Fixed submission:
lu = [2, 1]
x = int(input())
for i in range(2, x+1):
    lu.append(lu[i-1] + lu[i - 2])  
print(lu[x])

Buggy submission (verdict: Runtime Error):
l = [2, 1]

def l(x):
  if len(l) > x:
    return l[x]
  else:
    return l(x-1) + l(x-2)
  
print(l(int(input())))

Buggy submission (verdict: Runtime Error):
l = [2, 1]

def l(x):
  if len(l) > x:
    return l[x]
  else:
    return l(x-1) + l(x-2)
  
print(l(int(input()))
Problem p02761 (python)

Fixed submission:
import sys
N, M = map(int, input().split())
sc = [list(map(int, input().split())) for _ in range(M)]

if N == 1 and M == 0:
    print(0)
    sys.exit()
if N == 1 and sc[0][1] == 0:
    print(0)
    sys.exit()

A = [-1] * N
for i in range(M):
    if sc[i][0] == 1 and sc[i][1] == 0:
        print('-1')
        sys.exit()
    if A[sc[i][0] - 1] != -1 and A[sc[i][0] - 1] != sc[i][1]:
        print('-1')
        sys.exit()
    A[sc[i][0] - 1] = sc[i][1]
if A[0] == -1:
    A[0] = 1
for a in A:
    if a == -1:
        a = 0
    print(a, end='')
print('')

Buggy submission (verdict: Runtime Error):
import sys
N, M = map(int, input().split())
sc = [list(map(int, input().split())) for _ in range(M)]

if N == 1 and sc[0][1] == 0:
    print(0)
    sys.exit()

A = [-1] * N
A[0] = 1
for i in range(M):
    if sc[i][0] == 1 and sc[i][1] == 0:
        print('-1')
        sys.exit()
    if sc[i][0] != 1 and A[sc[i][0] - 1] != -1 and A[sc[i][0] - 1] != sc[i][1]:
        print('-1')
        sys.exit()
    A[sc[i][0] - 1] = sc[i][1]
for a in A:
    if a == -1:
        a = 0
    print(a, end='')
print('')
Problem p02796 (python)

Fixed submission:
N = int(input())
X = [list(map(int, input().split())) for i in range(N)]
LR = [[x[0]-x[1], x[0] + x[1]] for x in X]
LR.sort(key=lambda x:x[1])

A = 1
right = LR[0][1]
for i in range(1, N):
    if right <= LR[i][0]:
        A += 1
        right = LR[i][1]
print(A)

Buggy submission (verdict: Runtime Error):
N = int(input())
R = sorted([list(map(int, input().split())) for i in range(N)])
T = []
for i in range(N):
    T.append([R[i][0] + R[i][1], i])
T.sort()

while len(T) - 1 > 0:
    t = T.pop(0)
    print(t[0], R[t[1] + 1][0], R[t[1] + 1][1])
    if t[0] > R[t[1] + 1][0] - R[t[1] + 1][1]:
        N -= 1
        R[t[1] + 1][1] = 0
        T.pop(0)
print(N)
Problem p03044 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 6)
N = int(input())
uvw = [list(map(int, input().split())) for i in range(N - 1)]
R = [-1] * N
Q = [[] for i in range(N)]
for i in range(N - 1):
    Q[uvw[i][0] - 1].append([uvw[i][1], uvw[i][2]])
    Q[uvw[i][1] - 1].append([uvw[i][0], uvw[i][2]])

def func(i, s, d, l):
    
    if R[i] != -1:
        return

    if R[i] == -1:
        if l %2 == 0:
            R[i] = s
        else:
            R[i] = d
            tmp = s
            s = d
            d = tmp
    for j in range(len(Q[i])):
        func(Q[i][j][0] - 1, s, d, Q[i][j][1])

func(uvw[0][0] - 1, 0, 1, 0)
for i in range(len(R)):
    print(R[i])

Buggy submission (verdict: Runtime Error):
N = int(input())
uvw = [list(map(int, input().split())) for i in range(N - 1)]
R = [-1] * N
Q = [[] for i in range(N)]
for i in range(N - 1):
    Q[uvw[i][0] - 1].append([uvw[i][1], uvw[i][2]])
    Q[uvw[i][1] - 1].append([uvw[i][0], uvw[i][2]])

def func(i, s, d, l):
    
    if R[i] != -1:
        return

    if R[i] == -1:
        if l %2 == 0:
            R[i] = s
        else:
            R[i] = d
            s = d
            d = s
    for j in range(len(Q[i])):
        func(Q[i][j][0] - 1, s, d, Q[i][j][1])

func(uvw[0][0] - 1, 0, 1, 0)
for i in range(len(R)):
    print(R[i])

Buggy submission (verdict: Runtime Error):
N = int(input())
uvw = [list(map(int, input().split())) for i in range(N - 1)]
R = [-1] * N
Q = [[] for i in range(N)]
for i in range(N - 1):
    Q[uvw[i][0] - 1].append([uvw[i][1], uvw[i][2]])
    Q[uvw[i][1] - 1].append([uvw[i][0], uvw[i][2]])

def func(i, s, d, l):
    
    if R[i] != -1:
        return

    if R[i] == -1:
        if l %2 == 0:
            R[i] = s
        else:
            R[i] = d
            s = d
            d = s
    for j in range(len(Q[i])):
        func(Q[i][j][0] - 1, s, d, Q[i][j][1])

func(uvw[0][0] - 1, 0, 1, 0)
print(R)

Buggy submission (verdict: Runtime Error):
N = int(input())
uvw = [list(map(int, input().split())) for i in range(N - 1)]
R = [-1] * N
Q = [[] for i in range(N)]
for i in range(N - 1):
    Q[uvw[i][0] - 1].append([uvw[i][1], uvw[i][2]])
    Q[uvw[i][1] - 1].append([uvw[i][0], uvw[i][2]])
def func(i, s, d, l):
    
    if R[i] != -1:
        return

    if R[i] == -1:
        if l %2 == 0:
            R[i] = s
        else:
            R[i] = d
            s = d
            d = s
    for j in range(len(Q[i])):
        func(Q[i][j-1][0] - 1, s, d, Q[i][j-1][1])

func(uvw[0][0] - 1, 0, 1, Q[0][0][1])
print(R)
Problem p03361 (python)

Fixed submission:
import sys
H, W = map(int, input().split())
s = []
s.append('.' * (W + 2))
for i in range(1, H + 1):
    a = input()
    a = '.' + a + '.'
    s.append(a)
s.append('.' * (W + 2))
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if s[i][j] == '#':
            OK = False
            if s[i - 1][j] == '#' or s[i + 1][j] == '#' or s[i][j - 1] == '#' or s[i][j + 1] == '#':
                OK = True
            if OK == False:
                print('No')
                sys.exit()
print('Yes')

Buggy submission (verdict: Runtime Error):
import sys
H, W = map(int, input().split())
s = [input() for i in range(H)]
S = []
S.append('.' * (W + 2))
for i in range(W):
    S.append('.' + s[i] + '.')
S.append('.' * (W + 2))
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if S[i][j] == '#':
            if S[i + 1][j] == '#' or S[i - 1][j] == '#' or S[i][j + 1] == '#' or S[i][j - 1] == '#':
                continue
            else:
                print('No')
                sys.exit()
print('Yes')
Problem p03565 (python)

Fixed submission:
import sys
S = input()
T = input()
s = len(S)
t = len(T)
if s < t:
    print('UNRESTORABLE')
    sys.exit()

for i in range(s - t, -1, -1):
    OK = True
    for j in range(t):
        if S[i + j] != T[j] and S[i + j] != '?':
            OK = False
    if OK:
        for j in range(t):
            SS = S[:i] + T + S[i + j + 1:]
            SS = SS.replace('?', 'a')
        print(SS)
        sys.exit()

print('UNRESTORABLE')

Buggy submission (verdict: Runtime Error):
S = list(input())
T = list(input())
s = len(S)
t = len(T)
OK = False
P = 0
for i in range(s - t + 1):
    if S[i] != T[0] and S[i] != '?':
        continue
    for j in range(1, t):
        if S[i + j] != T[j] and S[i + j] != '?':
            break
        if j == t - 1:
            OK = True
            P = i
for k in range(t):
    S[P + k] = T[k]
for i in range(s):
    if S[i] == '?':
        S[i] = 'a'
if OK:
    [print(S[i], end='') for i in range(s)]
    print('')
else:
    print('UNRESTORABLE')

Buggy submission (verdict: Runtime Error):
S = list(input())
T = list(input())
s = len(S)
t = len(T)
OK = False
P = 0
for i in range(s - t + 1):
    if S[i] != T[0] and S[i] != '?':
        continue
    for j in range(1, t):
        if S[i + j] != T[j] and S[i + j] != '?':
            OK = False
            break
        if j == t - 1:
            OK = True
            P = i
for k in range(t):
    S[P + k] = T[k]
for i in range(s):
    if S[i] == '?':
        S[i] = 'a'
if OK:
    [print(S[i], end='') for i in range(s)]
    print('')
else:
    print('UNRESTORABLE')