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

Fixed submission:
N = int(input())
X = input()
X = X[::-1]

def count(n):
    if n==0:
        return 0
    m = 0
    for i in range(18):
        if n&2**i:
            m += 1
    n %= m
    return 1+count(n)



p1 = [1]*(N+1)
p2 = [1]*(N+1)
m = 0
for i in range(N):
    if X[i]=='1':
        m += 1

for n in range(N):
    p1[n+1] = 2*p1[n]%(m+1)
    if m!=1:
        p2[n+1] = 2*p2[n]%(m-1)

num = [0]*N
base1 = 0
base2 = 0
for i in range(N):
    if X[i]=='1':
        base1 += p1[i]
        base2 += p2[i]

ans = [0]*N
for i in range(N):
    if X[i]=='0':
        num[i] = (base1+p1[i])%(m+1)
        ans[i] += 1
    else:
        if m!=1:
            num[i] = (base2-p2[i])%(m-1)
            ans[i] += 1
        else:
            num[i] = 0



for i in range(N-1,-1,-1):
    ans[i] += count(num[i])
    print(ans[i])

Buggy submission (verdict: Runtime Error):
N = int(input())
X = input()
X = X[::-1]

def count(n):
    if n==0:
        return 0
    m = 0
    for i in range(18):
        if n&2**i:
            m += 1
    n %= m
    return 1+count(n)



p1 = [1]*(N+1)
p2 = [1]*(N+1)
m = 0
for i in range(N):
    if X[i]=='1':
        m += 1
for n in range(N):
    p1[n+1] = 2*p1[n]%(m+1)
    p2[n+1] = 2*p2[n]%(m-1)

num = [0]*N
base1 = 0
base2 = 0
for i in range(N):
    if X[i]=='1':
        base1 += p1[i]
        base2 += p2[i]

for i in range(N):
    if X[i]=='0':
        num[i] = (base1+p1[i])%(m+1)
    else:
        num[i] = (base2-p2[i])%(m-1)

ans = [1]*N

for i in range(N-1,-1,-1):
    ans[i] += count(num[i])
    print(ans[i])
Problem p02669 (python)

Fixed submission:
T = int(input())
for i in range(T):
    N,A,B,C,D = map(int,input().split())
    memo = {0:0,1:D}
    def f(n):
        if n in memo:
            return memo[n]
        else:
            res = D*n
            r2 = n%2
            r3 = n%3
            r5 = n%5
            res = min(res,f(n//2)+A+r2*D,f(n//3)+B+r3*D,f(n//5)+C+r5*D)
            res = min(res,f((n+1)//2)+A+(2-r2)*D,f((n+2)//3)+B+(3-r3)*D,f((n+4)//5)+C+(5-r5)*D)
            memo[n] = res
            return res
    print(f(N))

Buggy submission (verdict: Runtime Error):
T = int(input())
for i in range(T):
    N,A,B,C,D = map(int,input().split())
    memo = {0:0,1:D}
    def f(n):
        if n in memo:
            return memo[n]
        else:
            res = D*n
            r2 = n%2
            r3 = n%3
            r5 = n%5
            res = min(res,f(n//2)+A+r2*D,f(n//3)+B+r3*D,f(n//5)+C+r5*D)
            res = min(res,f((n+1)//2)+A+(2-r2)*D,f((n+2)//3+1)+B+(3-r3)*D,f((n+4)//5+1)+C+(5-r5)*D)
            memo[n] = res
            return res
    print(f(N))

Buggy submission (verdict: Runtime Error):
from math import ceil
T = int(input())
for i in range(T):
    N,A,B,C,D = map(int,input().split())
    memo = {0:0,1:D}
    def f(n):
        if n in memo:
            return memo[n]
        else:
            res = D*n
            r2 = n%2
            r3 = n%3
            r5 = n%5
            res = min(res,f(n//2)+A+r2*D,f(n//3)+B+r3*D,f(n//5)+C+r5*D)
            res = min(res,f(n//2+1)+A+(2+r2)*D,f(n//3+1)+B+(3+r3)*D,f(n//5+1)+C+(5+r5)*D)
            res = min(res,f(ceil(n/2))+A+(2-r2)*D,f(ceil(n/3))+B+(3-r3)*D,f(ceil(n/5))+C+(5-r5)*D)
            memo[n] = res
            return res
    print(f(N))
Problem p02698 (python)

Fixed submission:
from bisect import bisect_left as bisect
import sys
sys.setrecursionlimit(10**9)

N = int(input())
a = list(map(int,input().split()))
g = [[] for _ in range(N)]
for i in range(N-1):
    u,v = map(int,input().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)

ans = [0]*N
def dfs(t,f,l):
    res = 0
    i = bisect(l,a[t])
    if len(l)==i:
        l.append(a[t])
    else:
        res = [i,l[i]]
        l[i]=a[t]
    ans[t] = len(l)
    for v in g[t]:
        if v != f:
            back = dfs(v,t,l)
            if back:
                l[back[0]] = back[1]
            else:
                l.pop()
    return res
dfs(0,0,[])
for i in range(N):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
from bisect import bisect

N = int(input())
a = list(map(int,input().split()))
g = [[] for _ in range(N)]
for i in range(N-1):
    u,v = map(int,input().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)

ans = [0]*N
def dfs(t,f,l,seq):
    l = l.copy()
    seq = seq.copy()
    l.append(a[t])
    i = bisect(seq,l[-1])
    if len(seq)==i:
        seq.append(l[-1])
    else:
        seq[i]=l[-1]
    ans[t] = len(seq)
    for v in g[t]:
        if v != f:
            dfs(v,t,l,seq)
dfs(0,0,[],[])
for i in range(N):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
from bisect import bisect

N = int(input())
a = list(map(int,input().split()))
g = [[] for _ in range(N)]
for i in range(N-1):
    u,v = map(int,input().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)

ans = [0]*N
def dfs(t,f,l,seq):
    l = l.copy()
    seq = seq.copy()
    l.append(a[t])
    print(l,seq)
    i = bisect(seq,l[-1])
    if len(seq)==i:
        seq.append(l[-1])
    else:
        seq[i]=l[-1]
    ans[t] = len(seq)
    for v in g[t]:
        if v != f:
            dfs(v,t,l,seq)
dfs(0,0,[],[])
for i in range(N):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
from bisect import bisect

N = int(input())
a = list(map(int,input().split()))
g = [[] for _ in range(N)]
for i in range(N-1):
    u,v = map(int,input().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)

ans = [0]*N
def dfs(t,f,l,seq):
    l = l.copy()
    seq = seq.copy()
    l.append(a[t])
    print(l,seq)
    i = bisect(seq,l[-1])
    if len(seq)==i:
        seq.append(l[-1])
    else:
        seq[i]=l[-1]
    ans[t] = len(seq)
    for v in g[t]:
        if v != f:
            dfs(v,t,l,seq)
dfs(0,0,[],[])
for i in range(N):
    print(ans[i])
Problem p02716 (python)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
s = [0]*N
for i in range(N):
    s[i] = s[i-2]+A[i]

dp = [0]*N
dp[1] = max(A[0],A[1])
if N>=3:
    dp[2] = max(A[0],A[1],A[2])
for n in range(3,N):
    if n%2==1:
        dp[n] = max(A[n]+dp[n-2],s[n-1])
    else:
        dp[n] = max(A[n]+dp[n-2],A[n-1]+dp[n-3],s[n-2])

print(dp[-1])

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
s = [0]*N
for i in range(N):
    s[i] = s[i-2]+A[i]

dp = [0]*N
dp[1] = max(A[0],A[1])
dp[2] = max(A[0],A[1],A[2])
for n in range(3,N):
    if n%2==1:
        dp[n] = max(A[n]+dp[n-2],s[n-1])
    else:
        dp[n] = max(A[n]+dp[n-2],A[n-1]+dp[n-3],s[n-2])

print(dp[-1])

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
s = [0]*N
for i in range(N):
    s[i] = s[i-2]+A[i]

def f(n):
    if n==0:
        return 0
    elif n ==1:
        return max(A[0],A[1])
    elif n==2:
        return max(A[0],A[1],A[2])
    elif n%2==1:
        return max(A[n]+f(n-2),s[n-1])
    else:
        return max(A[n]+f(n-2),A[n-1]+f(n-3),s[n-2])

ans = f(N-1)
print(ans)
Problem p02728 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
mod = 10**9+7
fact = [1]*(2*10**5+1)
for n in range(1,2*10**5+1):
    fact[n] = n*fact[n-1]%mod
factinv = [1]*(2*10**5+1)
inv = [1]*(2*10**5+1)
for n in range(1,2*10**5+1):
    factinv[n] = pow(fact[n],mod-2,mod)
    inv[n] = pow(n,mod-2,mod)
def comb(n,k):
    return fact[n]*factinv[k]*factinv[n-k]%mod

N = int(input())
g = [[] for _ in range(N)]

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

p = [-1]*N
p[0] = 0
q = [0]
while q:
    i = q.pop()
    for j in g[i]:
        if p[j]==-1:
            p[j] = i
            q.append(j)

c = [[] for _ in range(N)]
for i in range(1,N):
    c[p[i]].append(i)

cn = [-1]*N
def rec(n):
    if cn[n] != -1:
        return cn[n]
    res = 0
    for i in c[n]:
        res += rec(i)+1
    cn[n] = res
    return res

cn[0] = rec(0)    

ans = [0]*N

def dfs(n):
    res = fact[cn[n]]
    for c in g[n]:
        if c==p[n]:
            continue
        res *= factinv[cn[c]+1]*dfs(c)
        res %= mod
    return res

ans[0] = dfs(0)

q = [0]
while q:
    i = q.pop()
    for j in g[i]:
        if ans[j]==0:
            ans[j] = ans[i]*(cn[j]+1)*inv[N-1-cn[j]]%mod
            q.append(j)

for i in range(N):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
mod = 10**9+7
fact = [1]*(2*10**5+1)
for n in range(1,2*10**5+1):
    fact[n] = n*fact[n-1]%mod
factinv = [1]*(2*10**5+1)
inv = [1]*(2*10**5+1)
for n in range(1,2*10**5+1):
    factinv[n] = pow(fact[n],mod-2,mod)
    inv[n] = pow(n,mod-2,mod)
def comb(n,k):
    return fact[n]*factinv[k]*factinv[n-k]%mod

N = int(input())
g = [[] for _ in range(N)]

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

p = [-1]*N
p[0] = 0
q = [0]
while q:
    i = q.pop()
    for j in g[i]:
        if p[j]==-1:
            p[j] = i
            q.append(j)

c = [[] for _ in range(N)]
for i in range(1,N):
    c[p[i]].append(i)

cn = [-1]*N
def rec(n):
    if cn[n] != -1:
        return cn[n]
    res = 0
    for i in c[n]:
        res += rec(i)+1
    cn[n] = res
    return res

cn[0] = rec(0)    

ans = [0]*N

def dfs(n):
    res = fact[cn[n]]
    for c in g[n]:
        if c==p[n]:
            continue
        res *= factinv[cn[c]+1]*dfs(c)
        res %= mod
    return res

ans[0] = dfs(0)

q = [0]
while q:
    i = q.pop()
    for j in g[i]:
        if ans[j]==0:
            ans[j] = ans[i]*(cn[j]+1)*inv[N-1-cn[j]]%mod
            q.append(j)

for i in range(N):
    print(ans[i])
Problem p02775 (python)

Fixed submission:
N = input()
N = N[::-1]
ans = 0
p = 0
for i in range(len(N)):
    p += int(N[i])
    if p<5:
        ans += p
        p = 0
    elif p==5:
        if i+1==len(N):
            ans += 5
            p = 0
        elif int(N[i+1])>=5:
            ans += 5
            p = 1
        else:
            ans += 5
            p = 0
    else:
        ans += 10-p
        p = 1
if p==1:
    ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
N = input()
N = N[::-1]
ans = 0
p = 0
for i in range(len(N)):
    p += int(N[i])
    if p<5:
        ans += p
        p = 0
    elif p==5:
        if int(N[i+1])>=5:
            ans += 5
            p = 1
        else:
            ans += 5
            p = 0
    else:
        ans += 10-p
        p = 1
if p==1:
    ans += 1
print(ans)
Problem p02782 (python)

Fixed submission:
r1,c1,r2,c2 = map(int,input().split())
mod = 10**9+7
fact = [1]*(2*10**6+11)

for n in range(2*10**6+10):
    fact[n+1] = ((n+1)*fact[n])%mod

factinv = [1]*(2*10**6+10)
for n in range(10**6+10):
    factinv[n] = pow(fact[n],mod-2,mod)

def comb(n,k):
    return (fact[n]*factinv[n-k]*factinv[k])%mod

def fs(r,c):
    res = 0
    for i in range(1,c+1):
        res += comb(i+1+r,r)-1
    return res

ans = fs(r2,c2)-fs(r1-1,c2)-fs(r2,c1-1)+fs(r1-1,c1-1)
ans %= mod
print(ans)

Buggy submission (verdict: Runtime Error):
r1,c1,r2,c2 = map(int,input().split())
mod = 10**9+7
fact = [1]*(2*10**6+1)

for n in range(2*10**6):
    fact[n+1] = ((n+1)*fact[n])%mod

factinv = [1]*(2*10**6+1)
for n in range(10**6+1):
    factinv[n] = pow(fact[n],mod-2,mod)

def comb(n,k):
    return (fact[n]*factinv[n-k]*factinv[k])%mod

def fs(r,c):
    res = 0
    for i in range(1,c+1):
        res += comb(i+1+r,r)-1
    return res

ans = fs(r2,c2)-fs(r1-1,c2)-fs(r2,c1-1)+fs(r1-1,c1-1)
ans %= mod
print(ans)
Problem p02798 (python)

Fixed submission:
from collections import defaultdict
from operator import itemgetter
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

ans = 1000
for n in range(2**N):
    n = bin(n)[2:]
    if n.count('1')%2!=0:
        continue
    n = '0'*(N-len(n))+n
    l = []
    for i in range(N):
        if n[i]=='0':
            l.append([A[i],i])
        else:
            l.append([B[i],i+1])
    inversion = 0
    for i in range(N):
        if inversion>=ans:
            break
        for j in range(i+1,N):
            if l[i][0]>l[j][0]:
                inversion += 1
    if inversion>=ans:
        continue
    l.sort(key=itemgetter(0))
    d = defaultdict(list)
    for i in range(N):
        v,j = l[i]
        if (i-j)%2==1:
            d[v].append(i)
    possible = True
    for v in d:
        if len(d[v])%2==1:
            possible = False
        else:
            for i in range(len(d[v])//2):
                if (d[v][2*i+1]-d[v][2*i])%2==1:
                    inversion += d[v][2*i+1]-d[v][2*i]
                else:
                    possible = False
                    break
        if not possible:
            break
    if possible:
        ans = min(ans,inversion)

if ans == 1000:
    ans = -1
print(ans)

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

ans = 1000
for n in range(2**N):
    n = bin(n)[2:]
    n = '0'*(N-len(n))+n
    l = []
    for i in range(N):
        if n[i]=='0':
            l.append([A[i],i,0])
        else:
            l.append([B[i],i,1])
    inversion = 0
    for i in range(N):
        if inversion>=ans:
            break
        for j in range(i+1,N):
            if l[i][0]>l[j][0]:
                inversion += 1
    if inversion>=ans:
        continue
    l.sort()
    d = defaultdict(list)
    for i in range(N):
        v,j,k = l[i]
        if (i-j+k)%2==1:
            d[v].append(i)
    possible = True
    for v in d:
        if len(d[v])%2==1:
            possible = False
        else:
            for i in range(len(d[v])//2):
                if (d[v][2*i+1]-d[v][2*i])%2==1:
                    inversion += d[v][2*i+1]-d[v][2*i]
                else:
                    possible = False
                    break
        if not possible:
            break
    if possible:
        ans = min(ans,inversion)

if ans == 1000):
    ans = -1
print(ans)
Problem p02814 (python)

Fixed submission:
from math import floor
from fractions import gcd
N,M = map(int,input().split())

def prime_decomposition(n):
  i = 2
  d = {2:0}
  while i * i <= n:
    while n % i == 0:
      n //= i
      if i not in d:
          d[i] = 0
      d[i] += 1
    i += 1
  if n > 1:
    if n not in d:
        d[n] = 1
  return d
A = list(map(int,input().split()))
for i in range(N):
    A[i] //= 2

l = A[0]
for i in range(1,N):
    l = (l*A[i])//gcd(l,A[i])
    if l>M:
        break
ans = floor((M/l+1)/2)
n = prime_decomposition(l)[2]
p2 = 2**n
for i in range(N):
    if A[i]%p2 != 0:
        ans = 0
        break
print(ans)

Buggy submission (verdict: Runtime Error):
from math import floor
N,M = map(int,input().split())
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
def prime_decomposition(n):
  i = 2
  d = {}
  while i * i <= n:
    while n % i == 0:
      n //= i
      if i not in d:
          d[i] = 0
      d[i] += 1
    i += 1
  if n > 1:
    if n not in d:
        d[n] = 1
  return d
A = list(map(int,input().split()))
n = prime_decomposition(A[0])[2]
p2 = 2**n
ans = -1
for i in range(N):
    if A[i]%p2==0:
        A[i] //= 2
    else:
        ans = 0
if ans != 0:
    l = A[0]
    for i in range(1,N):
        l = (l*A[i])//gcd(l,A[i])
    ans = floor((M/l+1)/2)
print(ans)
Problem p02822 (python)

Fixed submission:
from collections import defaultdict,deque
mod = 10**9+7
N = int(input())
g = [[] for _ in range(N)]
d = defaultdict(lambda:-1)
p2 = [1]*(N+1)
p2[1] = pow(2,mod-2,mod)
for i in range(N):
    p2[i+1] = (p2[i]*p2[1])%mod

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

q = deque([[a,b]])
while q:
    i,j = q.pop()
    s = 1
    f = True
    for k in g[j]:
        if k == i:
            continue
        if (j,k) not in d:
            if f:
                q.append([i,j])
            q.append([j,k])
            f = False
        else:
            s += d[(j,k)]
    if f:
        d[(i,j)] = s

q = deque([[b,a]])
while q:
    i,j = q.pop()
    s = 1
    f = True
    for k in g[j]:
        if k == i:
            continue
        if (j,k) not in d:
            if f:
                q.append([i,j])
            q.append([j,k])
            f = False
        else:
            s += d[(j,k)]
    if f:
        d[(i,j)] = s

ans = 1-p2[N]-N*p2[1]

for i in range(N):
    for j in g[i]:
        if j<i:
            continue
        n = max(d[(i,j)],d[(j,i)])
        ans += (1-p2[n])*(1-p2[N-n])
        ans %= mod

print(ans)

Buggy submission (verdict: Runtime Error):
mod = 10**9+7
p2 = [1]*(3*10**5)
for n in range(3*10**5-1):
    p2[n+1] = p2[n]*2%mod

N = int(input())
AB = [list(map(int,input().split())) for _ in range(N-1)]
g = [[] for _ in range(N+1)]
for i in range(N-1):
    a,b = AB[i]
    g[a].append(b)
    g[b].append(a)

l1 = [0]*(N+1)
l2 = [[] for _ in range(N+1)]

def rec(p,c):
    res = 0
    for a in g[c]:
        if a == p:
            continue
        n = rec(c,a)
        l2[c].append(n+1)
        res += n+1
    l1[c] = res
    return res

n = rec(0,1)
for i in range(1,N+1):
    l2[i].append(N-1-l1[i])

ans = 0
for i in range(1,N+1):
    ans += p2[N-1]-1
    ans %= mod
    for j in l2[i]:
        ans -= p2[j]-1
        ans %= mod
ans *= pow(p2[N],mod-2,mod)
ans %= mod
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
mod = 10**9+7
N = int(input())
g = [[] for _ in range(N)]
d = defaultdict(lambda:-1)
p2 = [1]*(N+1)
p2[1] = pow(2,mod-2,mod)
for i in range(N):
    p2[i+1] = (p2[i]*p2[1])%mod

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

def dfs(i,j):
    if d[(i,j)]!=-1:
        return d[(i,j)]
    res = 1
    for k in g[j]:
        if k==i:
            continue
        res += dfs(j,k)
    d[(i,j)] = res
    return res
dfs(a,b)
dfs(b,a)

ans = 1-p2[N]-N*p2[1]

for i in range(N):
    for j in g[i]:
        if j<i:
            continue
        n = max(d[(i,j)],d[(j,i)])
        ans += (1-p2[n])*(1-p2[N-n])
        ans %= mod

print(ans)
Problem p02824 (python)

Fixed submission:
N,M,V,P = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
def possible(i):
    A_ = [A[i] for i in range(N)]
    rest = M*(V-1)
    A_[i] += M
    for j in range(i+1,N):
        if A_[j] <= A_[i]:
            d = min(rest,A_[i]-A_[j])
            A_[j] += d
            rest -= d
        else:
            d = min(rest,M)
            A_[j] += d
            rest -= d
        if rest == 0:
            break
    for j in range(i):
        d = min(rest,M)
        A_[j] += d
        rest -= d
        if rest == 0:
            break
    for j in range(N-1,i,-1):
        if A_[j] == A_[i]:
            d = min(rest,A[j]+M-A_[j])
            A_[j] += d
            rest -= d
        if rest == 0:
            break
    c = A_[i]
    A_.sort(reverse=True)
    return A_[P-1] <= c

l = 0
r = N-1
m = (l+r)//2
for i in range(20):
    if possible(m):
        r = m
    else:
        l = m
    m = (l+r)//2
if possible(m):
    ans = N-m
else:
    ans = N-m-1
print(ans)

Buggy submission (verdict: Runtime Error):
N,M,V,P = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
def possible(i):
    A_ = A.copy()
    rest = M*(V-1)
    A_[i] += M
    for j in range(i+1,N):
        if A_[j] <= A_[i]:
            d = min(rest,A_[i]-A_[j])
            A_[j] += d
            rest -= d
        else:
            d = min(rest,M)
            A_[j] += d
            rest -= d
        if rest == 0:
            break
    for j in range(i):
        d = min(rest,M)
        A_[j] += d
        rest -= d
        if rest == 0:
            break
    for j in range(N-1,i,-1):
        if A_[j] == A_[i]:
            d = min(rest,A[j]+M-A_[j])
            A_[j] += d
            rest -= d
        if rest == 0:
            break
    c = A_[i]
    A_.sort(reverse=True)
    return A_[P-1] <= c

l = 0
r = N-1
m = (l+r)//2
for i in range(20):
    if possible(m):
        r = m
    else:
        l = m
    m = (l+r)//2
if possible(m):
    ans = N-m
else:
    ans = N-m-1
print(ans)
Problem p02855 (python)

Fixed submission:
H,W,K = map(int,input().split())
s = [input() for _ in range(H)]

a = [[0]*W for _ in range(H)]

cnt = 0

for h in range(H):
    for w in range(W):
        if s[h][w] == '#':
            cnt += 1
            a[h][w] = str(cnt)
            h_ = h-1
            while h_>=0 and a[h_][w]==0 and s[h_][w]=='.':
                a[h_][w] = str(cnt)
                h_ -= 1
            h_ = h+1
            while h_<=H-1 and a[h_][w]==0 and s[h_][w]=='.':
                a[h_][w] = str(cnt)
                h_ += 1

for h in range(H):
    for w in range(W):
        if a[h][w] != 0:
            cnt = int(a[h][w])
            w_ = w-1
            while w_>=0 and a[h][w_]==0:
                a[h][w_] = str(cnt)
                w_ -= 1
            w_ = w+1
            while w_<=W-1 and a[h][w_]==0:
                a[h][w_] = str(cnt)
                w_ += 1
    

for h in range(H):
    print(' '.join(a[h]))

Buggy submission (verdict: Runtime Error):
H,W,K = map(int,input().split())
s = [input() for _ in range(H)]

a = [[0]*W for _ in range(H)]

cnt = 0

for h in range(H):
    for w in range(W):
        if s[h][w] == '#':
            cnt += 1
            a[h][w] = str(cnt)
            h_ = h-1
            while h_>=0 and a[h_][w]==0 and s[h_][w]=='.':
                a[h_][w] = str(cnt)
                h_ -= 1
            h_ = h+1
            while h_<=H-1 and a[h_][w]==0 and s[h_][w]=='.':
                a[h_][w] = str(cnt)
                h_ += 1

for h in range(H):
    for w in range(W):
        if a[h][w] != 0:
            cnt = int(a[h][w])
            w_ = w-1
            while w_>=0 and a[h][w_]==0:
                a[h_][w] = str(cnt)
                w_ -= 1
            w_ = w+1
            while w_<=W-1 and a[h][w_]==0:
                a[h][w_] = str(cnt)
                w_ += 1
    

for h in range(H):
    print(''.join(a[h]))
Problem p02889 (python)

Fixed submission:
INF = float('inf')
N,M,L = map(int,input().split())
cost = [[INF]*N for _ in range(N)]
for i in range(M):
    a,b,c = map(int,input().split())
    cost[a-1][b-1] = c
    cost[b-1][a-1] = c

for k in range(N):
    for i in range(N):
        for j in range(N):
            if cost[i][j] > cost[i][k]+cost[k][j]:
                cost[i][j] = cost[i][k]+cost[k][j]

for i in range(N):
    for j in range(N):
        if cost[i][j] > L:
            cost[i][j] = INF
        else:
            cost[i][j] = 1

for k in range(N):
    for i in range(N):
        for j in range(N):
            if cost[i][j] > cost[i][k]+cost[k][j]:
                cost[i][j] = cost[i][k]+cost[k][j]

for i in range(N):
    for j in range(N):
        if cost[i][j] == INF:
            cost[i][j] = -1
        else:
            cost[i][j] -= 1

Q = int(input())

for i in range(Q):
    s,t = map(int,input().split())
    print(cost[s-1][t-1])

Buggy submission (verdict: Runtime Error):
from scipy.sparse.csgraph import floyd_warshall as fw
INF = float('inf')
N,M,L = map(int,input().split())
cost = [[INF]*N for _ in range(N)]
for i in range(M):
    a,b,c = map(int,input().split())
    cost[a-1][b-1] = c
    cost[b-1][a-1] = c

cost = [[0,1,2],[INF,0,2],[INF,INF,1]]

for i in range(N):
    for j in range(N):
        if cost[i][j] > L:
            cost[i][j] = INF
        else:
            cost[i][j] = 1

cost = fw(cost)

for i in range(N):
    for j in range(N):
        if cost[i][j] == INF:
            cost[i][j] = -1
        else:
            cost[i][j] -= 1

Q = int(input())

for i in range(Q):
    s,t = map(int,input().split())
    print(cost[s-1][t-1])