Problem p02762 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**5)
N,M,K = list(map(int,input().split(' ')))

friend_list = [0]*(N+1)
bad_list = [0]*(N+1)
group = {}

parent = [i for i in range(N+1)]
rank = [0]*(N+1)

def union(x,y):
    x = find(x)
    y = find(y)
    if x==y:
        return
    if rank[x]<rank[y]:
        parent[x]=y
    else:
        parent[y]=x
        if rank[x]==rank[y]:
            rank[x]+=1

def find(x):
    if parent[x] == x:
        return x
    else:
        parent[x]= find(parent[x])
        return parent[x]





for i in range(M):
    A,B = list(map(int,input().split(' ')))
    friend_list[A]+=1
    friend_list[B]+=1
    
    union(A,B)

for i in range(1,N+1):
    oya = find(i)
    if oya in group:
        group[oya] = group[oya]+1
    else:
        group[oya]=1

for i in range(K):
    A,B = list(map(int,input().split(' ')))
    if find(A)==find(B):
        bad_list[A]+=1
        bad_list[B]+=1


kouho = [0]*N

for i in range(1,N+1):
    oya = find(i)
    abc = group[oya]-friend_list[i]-bad_list[i]-1
    kouho[i-1] = abc

for k in kouho:
    print(k,end=" ")
print()

Buggy submission (verdict: Runtime Error):
N,M,K=map(int,input().split())
friend=[list(map(int,input().split())) for i in range(M)]
block=[list(map(int,input().split())) for i in range(K)]
newfriend=[]
def did(M):
    
    did=0
    for i in range(M):
       newfriend[i].sort()
    for i in range(1,M):
        did=0
        q,w=-1,-1
        for k in range(len(fchain)):
           if friend[i][0] in fchain[k]:
                q=k
           if friend[i][1] in fchain[k]:
                w=k
        for j in range(len(fchain)):
            if q!=w and q>=0 and w>=0:
                b=fchain.pop(max(q,w))
                fchain[min(q,w)]+=b
                did=1
                break
            elif (friend[i][0] in fchain[j]) and (friend[i][1] not in fchain[j]):
                fchain[j].append(friend[i][1])
                did=1
                break
            elif (friend[i][0] not in fchain[j]) and (friend[i][1] in fchain[j]):
                fchain[j].append(friend[i][0])
                did=1
                break
            elif (friend[i][0]  in fchain[j]) and (friend[i][1] in fchain[j]):
                did=1
                break
        if did==0:
            fchain.append(friend[i])

if M!=0 and K!=0:
    for i in range(K):
        block[i].sort()
    for i in range(M):
        newdata=friend[i].copy()
        newfriend.append(newdata)
    fchain=[friend[0]]
    did(M)
    ans=[]
    for i in range(1,N+1):
        count=0
        for j in range(len(fchain)):
            if i in fchain[j]:
                where=j
                fchain[j].sort()
                break
        for j in range(len(fchain[where])):
            need=[i,fchain[where][j]]
            need.sort()
            if i==fchain[where][j]:
                continue
            elif (need not in newfriend) and (need not in block):
                count+=1
        ans.append(count)
    print(" ".join(map(str,ans)))
elif M==0:
    print("0")
else:#M!=0 K==0
    for i in range(M):
        newdata=friend[i].copy()
        newfriend.append(newdata)
    fchain=[friend[0]]
    did(M)
    ans=[]
    for i in range(1,N+1):
        count=0
        for j in range(len(fchain)):
            if i in fchain[j]:
                where=j
                fchain[j].sort()
                break
        for j in range(len(fchain[where])):
            need=[i,fchain[where][j]]
            need.sort()
            if i==fchain[where][j]:
                continue
            elif (need not in newfriend):
                count+=1
        ans.append(count)
    print(" ".join(map(str,ans)))

Buggy submission (verdict: Runtime Error):
N,M,K=map(int,input().split())
friend=[list(map(int,input().split())) for i in range(M)]
block=[list(map(int,input().split())) for i in range(K)]
newfriend=[]
for i in range(M):
    newdata=friend[i].copy()
    newfriend.append(newdata)
fchain=[friend[0]]
did=0
for i in range(M):
    newfriend[i].sort()
for i in range(K):
    block[i].sort()
for i in range(1,M):
    did=0
    q,w=-1,-1
    for k in range(len(fchain)):
       if friend[i][0] in fchain[k]:
            q=k
       if friend[i][1] in fchain[k]:
            w=k
    for j in range(len(fchain)):
        if q!=w and q>=0 and w>=0:
            b=fchain.pop(max(q,w))
            fchain[min(q,w)]+=b
            did=1
            break
        elif (friend[i][0] in fchain[j]) and (friend[i][1] not in fchain[j]):
            fchain[j].append(friend[i][1])
            did=1
            break
        elif (friend[i][0] not in fchain[j]) and (friend[i][1] in fchain[j]):
            fchain[j].append(friend[i][0])
            did=1
            break
        elif (friend[i][0]  in fchain[j]) and (friend[i][1] in fchain[j]):
            did=1
            break
    if did==0:
        fchain.append(friend[i])
ans=[]
for i in range(1,N+1):
    count=0
    for j in range(len(fchain)):
        if i in fchain[j]:
            where=j
            fchain[j].sort()
            break
    for j in range(len(fchain[where])):
        need=[i,fchain[where][j]]
        need.sort()
        if i==fchain[where][j]:
            continue
        elif (need not in newfriend) and (need not in block):
            count+=1
    ans.append(count)
print(" ".join(map(str,ans)))

Buggy submission (verdict: Runtime Error):
N,M,K=map(int,input().split())
friend=[list(map(int,input().split())) for i in range(M)]
block=[list(map(int,input().split())) for i in range(K)]
newfriend=[]
for i in range(M):
    newdata=friend[i].copy()
    newfriend.append(newdata)
fchain=[friend[0]]
did=0
for i in range(M):
    newfriend[i].sort()
for i in range(K):
    block[i].sort()
for i in range(1,M):
    did=0
    for j in range(len(fchain)):
        q,w=-1,-1
        for k in range(len(fchain)):
            if friend[i][0] in fchain[k]:
                q=k
            if friend[i][1] in fchain[k]:
                w=k
        if q!=w and q>=0 and w>=0:
            b=fchain.pop(max(q,w))
            fchain[min(q,w)]+=b
            did=1
            break
        elif (friend[i][0] in fchain[j]) and (friend[i][1] not in fchain[j]):
            fchain[j].append(friend[i][1])
            did=1
            break
        elif (friend[i][0] not in fchain[j]) and (friend[i][1] in fchain[j]):
            fchain[j].append(friend[i][0])
            did=1
            break
        elif (friend[i][0]  in fchain[j]) and (friend[i][1] in fchain[j]):
            did=1
            break
    if did==0:
        fchain.append(friend[i])
ans=[]
for i in range(1,N+1):
    count=0
    for j in range(len(fchain)):
        if i in fchain[j]:
            where=j
            fchain[j].sort()
            break
    for j in range(len(fchain[where])):
        need=[i,fchain[where][j]]
        need.sort()
        if i==fchain[where][j]:
            continue
        elif (need not in newfriend) and (need not in block):
            count+=1
    ans.append(count)
print(" ".join(map(str,ans)))