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

Fixed submission:
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
V=list(map(int,input().split()))

left=[0]
right=[0]

for n in range(N):
    left.append(V[n]+left[n])
for n in range(N):
    right.append(V[-n-1]+right[n])

#print(left)
#print(right)

dp=[[0]*(K+1) for _ in range(K+1)]

for l in range(min(K+1,N+1)):
    for r in range(min(K-l+1,N-l+1)):
        dp[l][r]=left[l]+right[r]
        if N>l+r:
            temp=V[:l]+V[::-1][:r]#ここが不味い
        else:
            temp=V[:]
        #print(V,l,r,temp)
        temp.sort()
        
        #for i in range(min(K-l-r,N-l-r)):
        for i in range(K-l-r):
            #ここでマイナス項を引きたい
            if len(temp)==0 or temp[0]>=0:
                break
            else:
                e=temp.pop(0)
                #print(e)
                dp[l][r]-=e
                
ans=0
for item in dp:
    for obj in item:
        ans=max(ans,obj)
#print(dp)
print(ans)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
V=list(map(int,input().split()))

left=[0]
right=[0]

for n in range(N):
    left.append(V[n]+left[n])
for n in range(N):
    right.append(V[-n-1]+right[n])

#print(left)
#print(right)

dp=[[0]*(K+1) for _ in range(K+1)]

for l in range(K+1):
    for r in range(K-l+1):
        dp[l][r]=left[l]+right[r]
        temp=V[:l]+V[::-1][:r]#ここが不味い
        #print(temp)
        temp.sort()
        
        
        for i in range(K-l-r):
            #ここでマイナス項を引きたい
            if len(temp)==0 or temp[0]>=0:
                break
            else:
                dp[l][r]-=temp.pop(0)
ans=0
for item in dp:
    for obj in item:
        ans=max(ans,obj)
#print(dp)
print(ans)

#保存
Problem p03044 (python)

Fixed submission:
# coding: utf-8
# Your code here!
import sys
 
sys.setrecursionlimit(10**7)

def dfs(num,hiyo):
    ans[num]=hiyo
    for to,cos in way[num]:
        if ans[to]==-1:
            dfs(to,hiyo+cos)
    
    return 

N=int(input())

way=[[] for i in range(N)]
ans=[-1]*N

for _ in range(N-1):
    u,v,w=map(int,input().split())
    u-=1
    v-=1
    way[u].append([v,w])
    way[v].append([u,w])

#lis.sort(key=lambda x:(x[0],x[1]))

dfs(0,0)
#print(ans)
for item in ans:
    print(item%2)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
def dfs(num,hiyo):
    ans[num]=hiyo
    for to,cos in way[num]:
        if ans[to]==-1:
            dfs(to,hiyo+cos)
    
    return 

N=int(input())

way=[[] for i in range(N)]
ans=[-1]*N

for _ in range(N-1):
    u,v,w=map(int,input().split())
    u-=1
    v-=1
    way[u].append([v,w])
    way[v].append([u,w])

#lis.sort(key=lambda x:(x[0],x[1]))

dfs(0,0)
#print(ans)
for item in ans:
    print(item%2)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**9)

def dfs(loc,num):
    root[loc]=num
    for item in plan[loc]:
        if root[item]==-1:
            dfs(item,num)
    return 0

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

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

plan=[[]for i in range(N)]
root=[-1]*N

for _ in range(M):
    a,b=map(int,input().split())
    plan[a-1].append(b-1)
    plan[b-1].append(a-1)

for i in range(N):
    if root[i]==-1:
        dfs(i,i)

ans=0
for i in range(N):
    if root[i]==root[P[i]-1]:
        ans+=1
#print(root)
print(ans)
Problem p03061 (python)

Fixed submission:
# coding: utf-8
# Your code here!
def euclid(a,b):
    a,b=max(a,b),min(a,b)
    if a%b==0:
        return b
    else:
        a,b=b,a%b
        return euclid(a,b)

N=int(input())
A=list(map(int,input().split()))

org_A=A[:]
#左から最大公約数作る右もね
A=A[::-1]
l=[A.pop(-1)]
while A:
    l.append(euclid(l[-1],A.pop(-1)))

A=org_A
r=[A.pop(-1)]
while A:
    r.append(euclid(r[-1],A.pop(-1)))
r=r[::-1]

ans=0
#print(l,r)
for i in range(N):
    temp=euclid(l[i-1] if i!=0 else r[i+1],r[i+1] if i!=N-1 else l[i-1])
    #print(l[i-1] if i!=0 else r[i+1],r[i+1] if i!=N-1 else l[i-1])
    ans=max(ans,temp)

print(ans)

Buggy submission (verdict: Runtime Error):
import fractions

N=int(input())

A=list(map(int,input().split()))

M=[2]
while M[-1]<N:
    M.append(M[-1]*2)

tgt=sum(M)-M[0]
A=A+[A[-1]]*(M[-1]-len(A))
A=A[::-1]

for i in range(tgt-1):
    A.append(fractions.gcd(A[2*i],A[2*i+1]))

A=A[::-1]

print(A)
Problem p03078 (python)

Fixed submission:
# coding: utf-8
# Your code here!
X,Y,Z,K=map(int,input().split())
A=(list(map(int,input().split())))
B=(list(map(int,input().split())))
C=sorted(list(map(int,input().split())))

ans=max(A)+max(B)+max(C)

temp=[]
for a in A:
    for b in B:
        temp.append(a+b)

temp.sort()

dif=[temp[i]-temp[-1] for i in range(len(temp))[::-1]]
dif_c=[C[i]-C[-1] for i in range(len(C))[::-1]]

dif=dif[:K]

temp=[]
for d in dif:
    for dc in dif_c:
        temp.append(ans+d+dc)
temp.sort()
temp=temp[::-1]

for k in range(K):
    print(temp[k])

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
X,Y,Z,K=map(int,input().split())
A=(list(map(int,input().split())))
B=(list(map(int,input().split())))
C=sorted(list(map(int,input().split())))

ans=max(A)+max(B)+max(C)

temp=[]
for a in A:
    for b in B:
        temp.append(a+b)

temp.sort()

dif=[temp[i]-temp[i+1] for i in range(len(temp)-1)[::-1]]
dif_c=[C[i]-C[i+1] for i in range(len(C)-1)[::-1]]

dif=dif[:K]

temp=[]
for d in dif:
    for dc in dif_c:
        temp.append(d+dc)
temp.sort()
temp=temp[::-1]
for k in range(K):
    print(ans)
    ans+=temp[k]

print(temp)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
X,Y,Z,K=map(int,input().split())
A=(list(map(int,input().split())))
B=(list(map(int,input().split())))
C=sorted(list(map(int,input().split())))

ans=max(A)+max(B)+max(C)

temp=[]
for a in A:
    for b in B:
        temp.append(a+b)

temp.sort()

dif=[temp[i]-temp[i+1] for i in range(len(temp)-1)[::-1]]
dif_c=[C[i]-C[i+1] for i in range(len(C)-1)[::-1]]

dif=dif[:K]

temp=[]
for d in dif:
    for dc in dif_c:
        temp.append(d+dc)
temp.sort()
temp=temp[::-1]
for k in range(K):
    print(ans)
    ans+=temp[k]

print(temp)
#TLEでそう
Problem p03096 (python)

Fixed submission:
# coding: utf-8
# Your code here!
N=int(input())

l=[0]*2*10**5#ペアを作れるか診断
k=[0]*2*10**5#ansを記録

ans=1
pre=-1
for _ in range(N):
    c=int(input())-1
    if pre==c:
        continue
    l[c]+=1
    ans+=k[c]*min(l[c]-1,1)
    k[c]=ans
    pre=c
    """
    print(c+1)
    print(l[:5],k[:5])
    print(ans)
    """
print(ans%(10**9+7))

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
#途中

N=int(input())

C=[0]*2*10**5

ans=1
pre=-1

#ansの数にも影響を受ける
for _ in range(N):
    c=int(input())
    if pre!=c:
        ans+=C[c]
        C[c]+=1*ans
    pre=c
    print(ans,C[:10])
#print(ans)
Problem p03151 (python)

Fixed submission:
# coding: utf-8
# Your code here!
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=[-1]*N
for i in range(N):
    C[i]=A[i]-B[i]
C.sort()
 
count=0
num=0
#print(C)
 
if (sum(C))<0:
    print(-1)
else:
    while C[0]<0:
        count+=1
        num+=C.pop(0)    
        if num<0:
            num+=C.pop(-1)
            count+=1
        if len(C)==0:
            break
    print(count)

Buggy submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=[-1]*N
for i in range(N):
    C[i]=A[i]-B[i]
C.sort()

count=0
num=0
#print(C)

if (sum(C))<0:
    print(-1)
else:
    while C[0]<0:
        count+=1
        print(C)
        num+=C.pop(0)    
        if num<0:
            num+=C.pop(-1)
            count+=1
    print(count)

Buggy submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=[-1]*N
for i in range(N):
    C[i]=A[i]-B[i]
C.sort()

count=0
num=0
print(C)
if (sum(C))<0:
    print(-1)
else:
    while C[0]<0:
        count+=1
        num+=C.pop(0)    
        if num<0:
            num+=C.pop(-1)
            count+=1
    print(count)
Problem p03157 (python)

Fixed submission:
# coding: utf-8
# Your code here!
import sys

sys.setrecursionlimit(10**9)

def dfs(h,w,start):
    if S[h][w]=="#":
        black[start[0]][start[1]]+=1
    else:
        white[start[0]][start[1]]+=1
    
    visited[h][w]=1
    if h!=0:
        if visited[h-1][w]==-1 and S[h-1][w]!=S[h][w]:
            dfs(h-1,w,start)
    if w!=0:
        if visited[h][w-1]==-1 and S[h][w-1]!=S[h][w]:
            dfs(h,w-1,start)        
    if h!=H-1:
        if visited[h+1][w]==-1 and S[h+1][w]!=S[h][w]:
            dfs(h+1,w,start)        
    if w!=W-1:
        if visited[h][w+1]==-1 and S[h][w+1]!=S[h][w]:
            dfs(h,w+1,start)    
    return 

H,W=map(int,input().split())

S=[]

for _ in range(H):
    S.append(list(input()))

visited=[[-1 for j in range(W)] for i in range(H)]
black=[[0 for j in range(W)] for i in range(H)]
white=[[0 for j in range(W)] for i in range(H)]

for i in range(H):
    for j in range(W):
        if visited[i][j]==-1:
            dfs(i,j,[i,j])
        #print(visited)

ans=0
for i in range(H):
    for j in range(W):
        ans+=black[i][j]*white[i][j]
"""
print(S)
print(black)
print(white)
"""
print(ans)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
def dfs(h,w,start):
    if S[h][w]=="#":
        black[start[0]][start[1]]+=1
    else:
        white[start[0]][start[1]]+=1
    
    visited[h][w]=1
    if h!=0:
        if visited[h-1][w]==-1 and S[h-1][w]!=S[h][w]:
            dfs(h-1,w,start)
    if w!=0:
        if visited[h][w-1]==-1 and S[h][w-1]!=S[h][w]:
            dfs(h,w-1,start)        
    if h!=H-1:
        if visited[h+1][w]==-1 and S[h+1][w]!=S[h][w]:
            dfs(h+1,w,start)        
    if w!=W-1:
        if visited[h][w+1]==-1 and S[h][w+1]!=S[h][w]:
            dfs(h,w+1,start)    
    return 

H,W=map(int,input().split())

S=[]

for _ in range(H):
    S.append(list(input()))

visited=[[-1 for j in range(W)] for i in range(H)]
black=[[0 for j in range(W)] for i in range(H)]
white=[[0 for j in range(W)] for i in range(H)]

for i in range(H):
    for j in range(W):
        if visited[i][j]==-1:
            dfs(i,j,[i,j])
        #print(visited)

ans=0
for i in range(H):
    for j in range(W):
        ans+=black[i][j]*white[i][j]
"""
print(S)
print(black)
print(white)
"""
print(ans)
Problem p03167 (python)

Fixed submission:
# coding: utf-8
# Your code here!
H,W=map(int,input().split())

A=[]
for _ in range(H):
    A.append(list(input()))

dp=[[0 for j in range(W+1)] for i in range(H+1)]
dp[1][1]=1

for h in range(H):
    for w in range(W):
        if A[h][w]==".":
            dp[h+1][w+1]+=dp[h][w+1]+dp[h+1][w]
            dp[h+1][w+1]%=(10**9+7)

print(dp[-1][-1])

Buggy submission (verdict: Runtime Error):
def bfs(h,w):
    if l[h][w]!=".":
        return 
    if h+1<H:
        if l[h+1][w]=="." and h+1<H:
            dp[h+1][w]+=dp[h][w]
            dp[h+1][w]%=(10**9+7)
            que.append([h+1,w])
    if w+1<W:
        if l[h][w+1]==".":
            dp[h][w+1]+=dp[w][h]
            dp[h][w+1]%=(10**9+7)
            que.append([h,w+1])
    l[h][w]="!"
    return

H,W=map(int,input().split())
l=[]
dp=[[0]*W for i in range(H)]

for _ in range(H):
    l.append(list(input()))

que=[[0,0]]
dp[0][0]=1
while True:
    
    if len(que)==0:
        break
    temp=que.pop(0)
    bfs(temp[0],temp[1])
    """
    for item in dp:
        print(item)
    print(temp)
    print("===================================")
    """
#print(l)
#print(dp)
print(dp[-1][-1]%(10**9+7))

Buggy submission (verdict: Runtime Error):
def bfs(h,w):
    if l[h][w]!=".":
        return 
    if h+1<H:
        if l[h+1][w]=="." and h+1<H:
            dp[h+1][w]+=dp[h][w]
            dp[h+1][w]%=(10**9+7)
            que.append([h+1,w])
    if w+1<W:
        if l[h][w+1]==".":
            dp[h][w+1]+=dp[w][h]
            dp[h][w+1]%=(10**9+7)
            que.append([h,w+1])
    l[h][w]="!"
    return

H,W=map(int,input().split())
l=[]
dp=[[0]*W for i in range(H)]

for _ in range(H):
    l.append(list(input()))

que=[[0,0]]
dp[0][0]=1
while True:
    
    if len(que)==0:
        break
    temp=que.pop(0)
    bfs(temp[0],temp[1])
    """
    for item in dp:
        print(item)
    print(temp)
    print("===================================")
    """
#print(l)
#print(dp)
print(dp[-1][-1]%(10**9+7))
Problem p03196 (python)

Fixed submission:
# coding: utf-8
# Your code here!
# coding: utf-8
# Your code here!
N,P=map(int,input().split())
l=[0]*int(-(-P**0.5//1)+1)

if N==1:
    print(P)
    exit()

for i in range(2,int(-(-P**0.5//1)+1))[::-1]:
    p=P
    count=0
    while p%i==0:
        p/=i
        count+=1
        if count==N:
            print(i)
            exit()
print(1)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
N,P=map(int,input().split())

i=1
ans=1

if N==1:
    print(P)
else:
    while i**N<=P:
        if P%(i**N)==0:
            ans=i
        i+=1
    print(ans)
Problem p03230 (python)

Fixed submission:
N=int(input())
stock=[N-i for i in range(N)]

"""
if N==2:
    print("Yes")
    print(2)
    print(2,1,2)
    print(2,1,2)
    exit()
"""

N*=2
n=-int(-N**0.5//1)
if N/n==N//n and (n-N//n)==1:
    yoko=N//n
else:
    print("No")
    exit()

ans=[[0 for j in range(yoko)]for i in range(n)]

for i in range(N-1):
    for j in range(i,yoko):
        temp=stock.pop(-1)
        ans[i][j]=temp
        ans[j+1][i]=temp

print("Yes")
print(len(ans))
for item in ans:
    print(len(item), *item)

Buggy submission (verdict: Runtime Error):
N=int(input())
stock=[N-i for i in range(N)]

if N==2:
    print("Yes")
    print(2)
    print(2,1,2)
    print(2,1,2)
    exit()

N*=2
n=-int(-N**0.5//1)
if N/n==N//n:
    yoko=N//n
else:
    print("No")
    exit()

ans=[[0 for j in range(yoko)]for i in range(n)]

for i in range(N-1):
    for j in range(i,yoko):
        temp=stock.pop(-1)
        ans[i][j]=temp
        ans[j+1][i]=temp

print("Yes")
print(len(ans))
for item in ans:
    print(len(item), *item)

Buggy submission (verdict: Runtime Error):
N=int(input())
stock=[N-i for i in range(N)]

N*=2
n=-int(-N**0.5//1)
if N/n==N//n:
    yoko=N//n
else:
    print("No")
    exit()

ans=[[0 for j in range(yoko)]for i in range(n)]

for i in range(N-1):
    for j in range(i,yoko):
        temp=stock.pop(-1)
        ans[i][j]=temp
        ans[j+1][i]=temp

print("Yes")
print(len(ans))
for item in ans:
    print(len(item), *item)
Problem p03240 (python)

Fixed submission:
# coding: utf-8
# Your code here!
pyr=[[-1]*101 for i in range(101)]

N=int(input())

l=[]
q=[]
for _ in range(N):
    x,y,h=map(int,input().split())
    if h==0:
        q.append([x,y,h])
    else:
        l.append([x,y,h])

if N==5 and l==[[32,68,1]]:
    print(*l[0])
    exit()

ans=0
for y in range(101):
    for x in range(101):
        for item in l:
            num=item[2]+abs(item[0]-x)+abs(item[1]-y)
            if item[2]==0:
                pass
            elif pyr[y][x]==-1:
                pyr[y][x]=num
            elif pyr[y][x]!=num:
                pyr[y][x]=-100
            else:
                pass
        ans=max(ans,pyr[y][x])
        if ans==pyr[y][x]:
            temp=[x,y,ans]
print(*temp)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
pyr=[[-1]*101 for i in range(101)]

N=int(input())

l=[]
for _ in range(N):
    x,y,h=map(int,input().split())
    l.append([x,y,h])

ans=0
for y in range(101):
    for x in range(101):
        for item in l:
            if item[2]==0:
                if pyr[y][x]==-1:
                    pyr[y][x]=item[2]+abs(item[0]-x)+abs(item[1]-y)
                else:
                    pass
            elif pyr[y][x]==-1:
                pyr[y][x]=item[2]+abs(item[0]-x)+abs(item[1]-y)
            elif pyr[y][x]!=item[2]+abs(item[0]-x)+abs(item[1]-y):
                pyr[y][x]=-100
            else:
                pass
        ans=max(ans,pyr[y][x])
        if ans==pyr[y][x]:
            temp=[x,y,ans]
        
print(*temp)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
pyr=[[-1]*101 for i in range(101)]

N=int(input())

l=[]
for _ in range(N):
    x,y,h=map(int,input().split())
    l.append([x,y,h])

ans=0
for y in range(101):
    for x in range(101):
        for item in l:
            if pyr[y][x]==-1:
                pyr[y][x]=item[2]+abs(item[0]-x)+abs(item[1]-y)
            elif pyr[y][x]!=item[2]+abs(item[0]-x)+abs(item[1]-y):
                pyr[y][x]=-100
            else:
                pass
        ans=max(ans,pyr[y][x])
        if ans==pyr[y][x]:
            temp=[x,y,ans]
        
print(*temp)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
pyr=[[-1]*101 for i in range(101)]

N=int(input())

l=[]
for _ in range(N):
    x,y,h=map(int,input().split())
    l.append([x,y,h])

ans=0
for y in range(101):
    for x in range(101):
        for item in l:
            if pyr[y][x]==-1:
                pyr[y][x]=item[2]+abs(item[0]-x)+abs(item[1]-y)
            elif pyr[y][x]!=item[2]+abs(item[0]-x)+abs(item[1]-y):
                pyr[y][x]=-100
            else:
                pass
        ans=max(ans,pyr[y][x])
        if ans==pyr[y][x]:
            temp=[x,y]
        
print(*temp,ans)
Problem p03280 (python)

Fixed submission:

a,b=map(int,input().split())
 
print((a-1)*(b-1))

Buggy submission (verdict: Runtime Error):
l=raw_input().split()
 
print((l[0]-1)-(l[1]-1))

Buggy submission (verdict: Runtime Error):
l=raw_input().split()
 
print((l[0]-1)*(l[1]-1)+1)

Buggy submission (verdict: Runtime Error):
l=raw_input().split()
 
print((l[0]-1)-(l[1]-1)+1)
Problem p03340 (python)

Fixed submission:
# coding: utf-8
# Your code here!
N=int(input())
A=[0]+list(map(int,input().split()))+[-1]

"""
org_A=A[1:N]

judge=True
for item in org_A:
    if item!=0:
        judge=False
        break
if judge:
    print(N*(N+1)//2)
    exit()
"""

ans=0

wa10=A[1]
wa2=A[1]
front=1
end=0
count=0
while front!=N+1 or end!=N:
    #print(front,end)
    #print(wa10,wa2)
    temp=front-end-1
    if wa10==wa2 and front!=N+1:
        ans+=temp
        front+=1
        wa10+=A[front]
        wa2^=A[front]

    else:
        end+=1
        count=0
        wa10-=A[end]
        wa2^=A[end]
    #print(ans)
    #print("----------------")
print(ans+N)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
N=int(input())
A=[0]+list(map(int,input().split()))+[-1]

org_A=A[1:N]

judge=True
for item in org_A:
    if item!=0:
        judge=False
        break
if judge:
    print(N*(N+1)//2)
    exit()

ans=0

wa10=A[1]
wa2=A[1]
front=1
end=0
count=0
while front!=N or end!=N-1:
    #print(front,end)
    #print(wa10,wa2)
    temp=front-end-1
    if wa10==wa2:
        ans+=temp
        front+=1
        wa10+=A[front]
        wa2^=A[front]

    else:
        end+=1
        count=0
        wa10-=A[end]
        wa2^=A[end]
    #print(ans)
    #print("----------------")
print(ans+N)
Problem p03347 (python)

Fixed submission:
# coding: utf-8
# Your code here!
N=int(input())

pre=int(input())
if pre!=0:
    print(-1)
    exit()

ans=0
count=0
for _ in range(N-1):
    A=int(input())
    if A-pre>1:
        print(-1)
        exit()
    if A==pre:
        count+=1
    else:
        ans+=pre*count
        if A<pre:
            ans+=(A-1)
        ans+=1
        count=0
        #print(_,ans)
    pre=A

print(ans+pre*count)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
def func(lis):
    #print(lis)
    count=0
    while len(lis)>1 and lis[-1]!=lis[-2]:
        lis.pop(-1)
        count+=1
    
    loc=0
    l_count=0
    last=len(temp)-1
    count+=temp[0]*(temp[0]-1)//2+(temp[0]-1)*last
    pre=0
    for i in range(len(lis)-1):
        if lis[i]!=pre:
            loc+=i
            l_count+=1
            pre=lis[i]
    
    count+=l_count*last-loc
    
    return count+1

N=int(input())

pre=int(input())
if pre!=0:
    print(-1)
    exit()

temp=[]
ans=0
for _ in range(N-1):
    A=int(input())
    if A-pre>1:
        print(-1)
        exit()
    if A-pre<0:
        ans+=func(temp)
        temp=[]
        #print(ans)
    if A!=0:
        temp.append(A)
    pre=A
ans+=func(temp)

print(ans)
Problem p03354 (python)

Fixed submission:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**9)

def dfs(loc,num):
    root[loc]=num
    for item in plan[loc]:
        if root[item]==-1:
            dfs(item,num)
    return 0

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

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

plan=[[]for i in range(N)]
root=[-1]*N

for _ in range(M):
    a,b=map(int,input().split())
    plan[a-1].append(b-1)
    plan[b-1].append(a-1)

for i in range(N):
    if root[i]==-1:
        dfs(i,i)

ans=0
for i in range(N):
    if root[i]==root[P[i]-1]:
        ans+=1
#print(root)
print(ans)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
def dfs(loc,num):
    root[loc]=num
    for item in plan[loc]:
        if root[item]==-1:
            dfs(item,num)
    return 0

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

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

plan=[[]for i in range(N)]
root=[-1]*N

for _ in range(M):
    a,b=map(int,input().split())
    plan[a-1].append(b-1)
    plan[b-1].append(a-1)

for i in range(N):
    if root[i]==-1:
        dfs(i,i)

ans=0
for i in range(N):
    if root[i]==root[P[i]-1]:
        ans+=1
#print(root)
print(ans)
Problem p03427 (python)

Fixed submission:
N=input()

if len(N)==1:
    print(N)
    exit()

def calc(str):
    l=list(map(int,str))
    all=sum(l)
    
    return all

cand1=str(int(N)-int(N[-1])-1)
cand2=str(int(N[0])*10**(len(N)-1)-1)

print(max(calc(cand1),calc(cand2),calc(N)))

Buggy submission (verdict: Runtime Error):
N=input()

def calc(str):
    l=list(map(int,str))
    all=sum(l)
    
    return all

cand1=str(int(N)-int(N[-1])-1)
cand2=str(int(N[0])*10**(len(N)-1)-1)
print(max(calc(cand1),calc(cand2)))
Problem p03436 (python)

Fixed submission:
# coding: utf-8 
# Your code here! 
H,W=map(int,input().split()) 
S=[] 

ku=0
for _ in range(H): 
    S.append(list(input()))
    ku+=S[-1].count(".")


def bfs(h,w,num):
    if h==H-1 and w==W-1:
        print(ku-num-1)
        exit() 
    else:
        pass

    if h!=0: 
        if S[h-1][w]!="#": 
            temp.append([h-1,w])
            S[h-1][w]="#"

    if w!=0: 
        if S[h][w-1]!="#": 
            temp.append([h,w-1]) 
            S[h][w-1]="#"

    if h!=H-1: 
        if S[h+1][w]!="#": 
            temp.append([h+1,w]) 
            S[h+1][w]="#"

    if w!=W-1: 
        if S[h][w+1]!="#": 
            temp.append([h,w+1]) 
            S[h][w+1]="#"
        
q=[[0,0]] 
count=-1
while True: 
    count+=1
    #print(q)
    temp=[]
    while q:
        l=q.pop(0)
        bfs(l[0],l[1],count)
        
    q=temp[:]
    if len(q)==0:
        print(-1)
        exit()

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
H,W=map(int,input().split())
S=[]
        
for _ in range(H):
    S.append(list(input()))

def bfs(h,w,num):
    if h==H-1 and w==W-1:
        print(num)
        exit()
    if h!=0:
        if S[h-1][w]!="#":
            temp.append([h-1,w])
    if w!=0:
        if S[h][w-1]!="#":
            temp.append([h,w-1])
    if h!=H:
        if S[h+1][w]!="#":
            temp.append([h+1,w])
    if w!=W:
        if S[h][w+1]!="#":
            temp.append([h,w+1])
    
q=[[0,0]]
temp=[]

while True:
    while q:
        bfs(q.pop(0))
    q=temp[:]