Problem p02931 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def par(a):
    if P[a] < 0: return a
    t = par(P[a])
    P[a] = t
    return t
def unite(a, b):
    if par(a) == par(b): return 0
    C[par(a)], C[par(b)] = C[par(a)] + C[par(b)], C[par(a)] + C[par(b)]
    if P[par(b)] == P[par(a)]:
        P[par(b)] = par(a)
        P[par(a)] -= 1
    elif P[par(b)] > P[par(a)]:
        P[par(b)] = par(a)
    else:
        P[par(a)] = par(b)
N, H, W = map(int, input().split())
P = [-1 for i in range(H+W)]
C = [1] * (H+W)
X = []
for _ in range(N):
    r, c, a = map(int, input().split())
    X.append((a, r-1, c+H-1))
X = sorted(X)
ans = 0
while X:
    a, r, c = X.pop()
    unite(r, c)
    if C[par(r)]:
        ans += a
        C[par(r)] -= 1
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
N, H, W = map(int, input().split())
P = [-1 for i in range(H+W)]
C = [1] * (H+W)
arr = []
for _ in range(N):
    r, c, a = map(int, input().split())
    arr.append((a, r-1, c+H-1))
arr = sorted(arr)


def find(a):
    if P[a] < 0: return a
    t = find(P[a])
    P[a] = t
    return t

def unite(r, c):
    fr, fc = find(r), find(c)
    if fr == fc:  return 0
    C[fr] = C[fc] = C[fr] + C[fc]
    if P[fr] == P[fc]:
        P[fc] = fr
        P[fr] -= 1
    elif P[fr] > P[fc]:
        P[fc] = fr
    else:
        P[fr] = fc
    
while arr:
    a, r, c = arr.pop()
    unite(r, c)
    fr = find(r)
    if C[fr]:
        ans += a
        C[fr] -= 1
print(ans)

Buggy submission (verdict: Runtime Error):
# sys.setrecursionlimit(10**6)

N, H, W = [int(x) for x in raw_input().split(' ')]
arr = []
for _ in xrange(N):
    r, c, a = [int(x) for x in raw_input().split(' ')]
    arr.append((a, r-1, c+H-1))
arr = sorted(arr)
ans = 0
P = [-1] * (H+W)
C = [1] * (H+W)

def find(a):
    if P[a] < 0: return a
    t = find(P[a])
    P[a] = t
    return t

def unite(r, c):
    fr, fc = find(r), find(c)
    if fr == fc:  return 0
    C[fr] = C[fc] = C[fr] + C[fc]
    if P[fr] == P[fc]:
        P[fc] = fr
        P[fr] -= 1
    elif P[fr] > P[fc]:
        P[fc] = fr
    else:
        P[fr] = fc
    
while arr:
    a, r, c = arr.pop()
    unite(r, c)
    fr = find(r)
    if C[fr]:
        ans += a
        C[fr] -= 1
print(ans)

Buggy submission (verdict: Runtime Error):
sys.setrecursionlimit(10**6)

N, H, W = [int(x) for x in raw_input().split(' ')]
arr = []
for _ in xrange(N):
    r, c, a = [int(x) for x in raw_input().split(' ')]
    arr.append((a, r-1, c+H-1))
arr = sorted(arr)
ans = 0
P = [-1] * (H+W)
C = [1] * (H+W)

def find(a):
    if P[a] < 0: return a
    t = find(P[a])
    P[a] = t
    return t

def unite(r, c):
    fr, fc = find(r), find(c)
    if fr == fc:  return 0
    C[fr] = C[fc] = C[fr] + C[fc]
    if P[fr] == P[fc]:
        P[fc] = fr
        P[fr] -= 1
    elif P[fr] > P[fc]:
        P[fc] = fr
    else:
        P[fr] = fc
    
while arr:
    a, r, c = arr.pop()
    unite(r, c)
    fr = find(r)
    if C[fr]:
        ans += a
        C[fr] -= 1
print(ans)

Buggy submission (verdict: Runtime Error):
N, H, W = [int(x) for x in raw_input().split(' ')]
arr = []
for _ in xrange(N):
    r, c, a = [int(x) for x in raw_input().split(' ')]
    arr.append((a, r-1, c+H-1))
arr = sorted(arr)
ans = 0
P = [-1] * (H+W)
C = [1] * (H+W)

def find(a):
    if P[a] < 0:    return a
    t = find(P[a])
    P[a] = t
    return t

def unite(a, b):
    if par(a) == par(b): return 0
    C[par(a)], C[par(b)] = C[par(a)] + C[par(b)], C[par(a)] + C[par(b)]
    if P[par(b)] == P[par(a)]:
        P[par(b)] = par(a)
        P[par(a)] -= 1
    elif P[par(b)] > P[par(a)]:
        P[par(b)] = par(a)
    else:
        P[par(a)] = par(b)
    
while arr:
    a, r, c = arr.pop()
    unite(r, c)
    if C[find(r)]:
        ans += a
        C[find(r)] -= 1
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def par(a):
    if P[a] < 0: return a
    t = par(P[a])
    P[a] = t
    return t
def unite(a, b):
    if par(a) == par(b): return 0
    C[par(a)], C[par(b)] = C[par(a)] + C[par(b)], C[par(a)] + C[par(b)]
    if P[par(b)] == P[par(a)]:
        P[par(b)] = par(a)
        P[par(a)] -= 1
    elif P[par(b)] > P[par(a)]:
        P[par(b)] = par(a)
    else:
        P[par(a)] = par(b)
N, H, W = map(int, input().split())
P = [-1 for i in range(H+W)]
C = [1] * (H+W)
X = []
for _ in range(N):
    r, c, a = map(int, input().split())
    X.append((a, r-1, c+H-1))
X = sorted(X)
ans = 0
while X:
    a, r, c = X.pop()
    unite(r, c)
    if C[par(r)]:
        ans += a
        C[par(r)] -= 1
print(ans

Buggy submission (verdict: Runtime Error):
sys.setrecursionlimit(10**6)

N, H, W = [int(x) for x in raw_input().split(' ')]
arr = []
for _ in xrange(N):
    r, c, a = [int(x) for x in raw_input().split(' ')]
    arr.append((a, r-1, c+H-1))
arr = sorted(arr)
ans = 0
P = [-1] * (H+W)
C = [1] * (H+W)

def find(a):
    if P[a] < 0:    return a
    t = find(P[a])
    P[a] = t
    return t

def unite(r, c):
    fr, fc = find(r), find(c)
    if fr == fc:  return 0
    C[fr] = C[fc] = C[fr] + C[fc]
    if P[fr] == P[fc]:
        P[fc] = fr
        P[fr] -= 1
    elif P[fr] > P[fc]:
        P[fc] = fr
    else:
        P[fr] = fc
    
while arr:
    a, r, c = arr.pop()
    unite(r, c)
    fr = find(r)
    if C[fr]:
        ans += a
        C[fr] -= 1
print(ans)

Buggy submission (verdict: Runtime Error):
N, H, W = [int(x) for x in raw_input().split(' ')]
arr = []
for _ in xrange(N):
    r, c, a = [int(x) for x in raw_input().split(' ')]
    arr.append((a, r-1, c+H-1))
arr = sorted(arr)
ans = 0
P = [-1] * (H+W)
C = [1] * (H+W)

def find(a):
    if P[a] < 0:    return a
    t = find(P[a])
    P[a] = t
    return t

def unite(r, c):
    fr, fc = find(r), find(c)
    if fr == fc:  return 0
    C[fr] = C[fc] = C[fr] + C[fc]
    if P[fr] == P[fc]:
        P[fc] = fr
        P[fr] -= 1
    elif P[fr] > P[fc]:
        P[fc] = fr
    else:
        P[fr] = fc
    
while arr:
    a, r, c = arr.pop()
    unite(r, c)
    fr = find(r)
    if C[fr]:
        ans += a
        C[fr] -= 1
print(ans)

Buggy submission (verdict: Runtime Error):
N, H, W = [int(x) for x in raw_input().split(' ')]
arr = []
for _ in xrange(N):
    r, c, a = [int(x) for x in raw_input().split(' ')]
    arr.append((a, r-1, c+H-1))
arr = sorted(arr)
ans = 0
P = [-1] * (H+W)
C = [1] * (H+W)

def find(a):
    if P[a] < 0:    return a
    t = find(P[a])
    P[a] = t
    return t

def unite(r, c):
    fr, fc = find(r), find(c)
    if fr == fc:  return 0
    C[fr] = C[fc] = C[fr] + C[fc]
    if P[fr] == P[fc]:
        P[fc] = fr
        P[fr] -= 1
    elif P[fr] > P[fc]:
        P[fc] = fr
    else:
        P[fr] = fc
    
while arr:
    a, r, c = arr.pop()
    unite(r, c)
    if C[find(r)]:
        ans += a
        C[find(r)] -= 1
print(ans)