Problem p02762 (python)

Fixed submission:
# スペース区切りの整数入力
N,M,K = map(int, input().split())

fcnum = [ 0 for i in range(N+1)]
fring = [[] for i in range(N+1)]
frno =  [-1 for i in range(N+1)]

ab = []
for i in range(M):
    aa,bb = map(int, input().split())
    if aa > bb:
        aa,bb = bb,aa
    fcnum[aa] -= 1
    fcnum[bb] -= 1
    ab.append([aa,bb])

for aa,bb in ab:
    if frno[aa] == -1 and frno[bb] == -1:
        frn = aa
        frno[aa] = frn
        frno[bb] = frn
        fring[frn].append(aa)
        fring[frn].append(bb)
    elif frno[aa] == -1 and frno[bb] != -1:
        frn = frno[bb]
        frno[aa] = frn
        fring[frn].append(aa)
    elif frno[aa] != -1 and frno[bb] == -1:
        frn = frno[aa]
        frno[bb] = frn
        fring[frn].append(bb)
    else:
        if frno[aa] != frno[bb]:
            if len(fring[frno[aa]]) > len(fring[frno[bb]]):
                frnto = frno[aa]
                frnfrom = frno[bb]
            else:
                frnto = frno[bb]
                frnfrom = frno[aa]
            fring[frnto].extend(fring[frnfrom])
            for mvf in fring[frnfrom]:
                frno[mvf] = frnto
            fring[frnfrom] = []

for i in range(K):
    cc,dd = map(int, input().split())
    if frno[cc] == frno[dd] and frno[cc] != -1:
        fcnum[cc] -= 1
        fcnum[dd] -= 1

for k in range(1,len(fring)):
    cnum = len(fring[k]) - 1
    for ff in fring[k]:
        fcnum[ff] += cnum

for i in range(1,len(fcnum)):
    print(fcnum[i]," ",end="")

Buggy submission (verdict: Runtime Error):
#import time
#start = time.time()

# スペース区切りの整数入力
N,M,K = map(int, input().split())

fcnum = [ 0 for i in range(N+1)]
fring = [[] for i in range(N+1)]
frno =  [-1 for i in range(N+1)]

ab = []
for i in range(M):
    aa,bb = map(int, input().split())
    if aa > bb:
        aa,bb = bb,aa
    fcnum[aa] -= 1
    fcnum[bb] -= 1
    ab.append([aa,bb])

for aa,bb in ab:
    if frno[aa] == -1 and frno[bb] == -1:
        frn = aa
        frno[aa] = frn
        frno[bb] = frn
        fring[frn].append(aa)
        fring[frn].append(bb)
    elif frno[aa] == -1 and frno[bb] != -1:
        frn = frno[bb]
        frno[aa] = frn
        fring[frn].append(aa)
    elif frno[aa] != -1 and frno[bb] == -1:
        frn = frno[aa]
        frno[bb] = frn
        fring[frn].append(bb)
    else:
        if frno[aa] != frno[bb]:
            if len(fring[frno[aa]]) > len(fring[frno[bb]]):
                frnto = frno[aa]
                frnfrom = frno[bb]
            else:
                frnto = frno[bb]
                frnfrom = frno[aa]
            fring[frnto].extend(fring[frnfrom])
            for mvf in fring[frnfrom]:
                frno[mvf] = frnto
            del fring[frnfrom]
            #fring[frnfrom] = []

for i in range(K):
    cc,dd = map(int, input().split())
    if frno[cc] == frno[dd]:
        fcnum[cc] -= 1
        fcnum[dd] -= 1

for k in range(1,len(fring)):
    cnum = len(fring[k]) - 1
    for ff in fring[k]:
        fcnum[ff] += cnum

for i in range(1,len(fcnum)):
    print(fcnum[i]," ",end="")

Buggy submission (verdict: Runtime Error):
# スペース区切りの整数入力
N,M,K = map(int, input().split())

fcnum = [ 0 for i in range(N+1)]
fring = [[] for i in range(N+1)]
frno =  [-1 for i in range(N+1)]

ab = []
for i in range(M):
    aa,bb = map(int, input().split())
    if aa > bb:
        aa,bb = bb,aa
    #ab.append([aa,bb])
    fcnum[aa] -= 1
    fcnum[bb] -= 1
    #for aa,bb in ab:
    if frno[aa] == -1 and frno[bb] == -1:
        frn = min(aa,bb)
        frno[aa] = frn
        frno[bb] = frn
        fring[frn].append(aa)
        fring[frn].append(bb)
    elif frno[aa] == -1 and frno[bb] != -1:
        frn = frno[bb]
        frno[aa] = frn
        fring[frn].append(aa)
    elif frno[aa] != -1 and frno[bb] == -1:
        frn = frno[aa]
        frno[bb] = frn
        fring[frn].append(bb)
    else:
        if frno[aa] != frno[bb]:
            if len(fring[aa]) > len(fring[bb]):
                frnto = frno[aa]
                frnfrom = frno[bb]
            else:
                frnto = frno[bb]
                frnfrom = frno[aa]
            fring[frnto].extend(fring[frnfrom])
            for mvf in fring[frnfrom]:
                frno[mvf] = frnto
            del fring[frnfrom]

for k in range(len(fring)):
    cnum = len(fring[k]) - 1
    for ff in fring[k]:
        fcnum[ff] += cnum

cd = []
for i in range(K):
    cc,dd = map(int, input().split())
    #if cc > dd:
    #    cc,dd = dd,cc
    #cd.append([cc,dd])
    if frno[cc] == frno[dd]:
        fcnum[cc] -= 1
        fcnum[dd] -= 1

for i in range(1,len(fcnum)):
    print(fcnum[i]," ",end="")

Buggy submission (verdict: Runtime Error):
# スペース区切りの整数入力
N,M,K = map(int, input().split())

fmap = [[0 for i2 in range(N)] for i1 in range(N)]

ab = []
for i in range(M):
    aa,bb = map(int, input().split())
    if aa > bb:
        aa,bb = bb,aa
    ab.append([aa,bb])
    fmap[aa-1][bb-1] = 1

for i in range(K):
    cc,dd = map(int, input().split())
    if cc > dd:
        cc,dd = dd,cc
    fmap[cc-1][dd-1] = -1

fring =  [[] for i in range(N+1)]
frno =  [-1 for i in range(N+1)]
fcnum = [ 0 for i in range(N+1)]

for aa,bb in ab:
    if frno[aa] == -1 and frno[bb] == -1:
        frn = min(aa,bb)
        frno[aa] = frn
        frno[bb] = frn
        fring[frn].append(aa)
        fring[frn].append(bb)
    elif frno[aa] == -1 and frno[bb] != -1:
        frn = frno[bb]
        frno[aa] = frn
        fring[frn].append(aa)
    elif frno[aa] != -1 and frno[bb] == -1:
        frn = frno[aa]
        frno[bb] = frn
        fring[frn].append(bb)
    else:
        if frno[aa] != frno[bb]:
            if len(fring[aa]) > len(fring[bb]):
                frnto = frno[aa]
                frnfrom = frno[bb]
            else:
                frnto = frno[bb]
                frnfrom = frno[aa]
            fring[frnto].extend(fring[frnfrom])
            for mvf in fring[frnfrom]:
                frno[mvf] = frnto
            del fring[frnfrom]

for k in range(len(fring)):
    for i in range(len(fring[k])-1):
        for j in range(i+1,len(fring[k])):
            f1 = fring[k][i]
            f2 = fring[k][j]
            if f1 > f2:
                f1,f2 = f2,f1
            if fmap[f1-1][f2-1] == 0:
                fcnum[f1] += 1
                fcnum[f2] += 1
                fmap[f1-1][f2-1] = frno[f1]

for i in range(1,len(fcnum)):
    print(fcnum[i]," ",end="")

Buggy submission (verdict: Runtime Error):
# スペース区切りの整数入力
N,M,K = map(int, input().split())

fmap = [[0 for i2 in range(N)] for i1 in range(N)]

ab = []
for i in range(M):
    aa,bb = map(int, input().split())
    if aa > bb:
        aa,bb = bb,aa
    ab.append([aa,bb])
    fmap[aa-1][bb-1] = 1

for i in range(K):
    cc,dd = map(int, input().split())
    if cc > dd:
        cc,dd = dd,cc
    fmap[cc-1][dd-1] = -1

fring =  [[] for i in range(N+1)]
frno =  [-1 for i in range(N+1)]
fcnum = [ 0 for i in range(N+1)]

for aa,bb in ab:
    if frno[aa] == -1 and frno[bb] == -1:
        frn = min(aa,bb)
        frno[aa] = frn
        frno[bb] = frn
        fring[frn].append(aa)
        fring[frn].append(bb)
    elif frno[aa] == -1 and frno[bb] != -1:
        frn = frno[bb]
        frno[aa] = frn
        fring[frn].append(aa)
    elif frno[aa] != -1 and frno[bb] == -1:
        frn = frno[aa]
        frno[bb] = frn
        fring[frn].append(bb)
    else:
        if frno[aa] != frno[bb]:
            if len(fring[aa]) > len(fring[bb]):
                frnto = frno[aa]
                frnfrom = frno[bb]
            else:
                frnto = frno[bb]
                frnfrom = frno[aa]
            for mvf in fring[frnfrom]:
                fring[frnto].append(mvf)
                frno[mvf] = frnto
            del fring[frnfrom]

for k in range(len(fring)):
    for i in range(len(fring[k])-1):
        for j in range(i+1,len(fring[k])):
            f1 = fring[k][i]
            f2 = fring[k][j]
            if f1 > f2:
                f1,f2 = f2,f1
            if fmap[f1-1][f2-1] == 0:
                fcnum[f1] += 1
                fcnum[f2] += 1
                fmap[f1-1][f2-1] = frno[f1]

for i in range(1,len(fcnum)):
    print(fcnum[i]," ",end="")

Buggy submission (verdict: Runtime Error):
# スペース区切りの整数入力
N,M,K = map(int, input().split())

fmap = [[0 for i2 in range(N)] for i1 in range(N)]

ab = []
for i in range(M):
    aa,bb = map(int, input().split())
    if aa > bb:
        aa,bb = bb,aa
    ab.append([aa,bb])
    fmap[aa-1][bb-1] = 1

for i in range(K):
    cc,dd = map(int, input().split())
    if cc > dd:
        cc,dd = dd,cc
    fmap[cc-1][dd-1] = -1

fring =  [[] for i in range(N+1)]
frno =  [-1 for i in range(N+1)]
fcnum = [ 0 for i in range(N+1)]

for aa,bb in ab:
    if frno[aa] == -1 and frno[bb] == -1:
        frn = min(aa,bb)
        frno[aa] = frn
        frno[bb] = frn
        fring[frn].append(aa)
        fring[frn].append(bb)
    elif frno[aa] == -1 and frno[bb] != -1:
        frn = frno[bb]
        frno[aa] = frn
        fring[frn].append(aa)
    elif frno[aa] != -1 and frno[bb] == -1:
        frn = frno[aa]
        frno[bb] = frn
        fring[frn].append(bb)
    else:
        if frno[aa] != frno[bb]:
            if len(fring[aa]) > len(fring[bb]):
                frnto = frno[aa]
                frnfrom = frno[bb]
            else:
                frnto = frno[bb]
                frnfrom = frno[aa]
            for mvf in fring[frnfrom]:
                fring[frnto].append(mvf)
                frno[mvf] = frnto
            del fring[frnfrom]

for k in range(len(fring)):
    for i in range(len(fring[k])-1):
        for j in range(i+1,len(fring[k])):
            f1 = fring[k][i]
            f2 = fring[k][j]
            if f1 > f2:
                f1,f2 = f2,f1
            if fmap[f1-1][f2-1] == 0:
                fcnum[f1] += 1
                fcnum[f2] += 1
                fmap[f1-1][f2-1] = frno[f1]

for i in range(1,len(fcnum)):
    print(fcnum[i]," ",end="")