Problem: There are N dots in a two-dimensional plane. The coordinates of the i-th dot are (x_i, y_i).  We will repeat the following operation as long as possible:    * Choose four integers a, b, c, d (a \neq c, b \neq d) such that there are dots at exactly three of the positions (a, b), (a, d), (c, b) and (c, d), and add a dot at the remaining position.  We can prove that we can only do this operation a finite number of times. Find the maximum number of times we can do the operation.

Submission attempts (in order):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

class UnionFind:
    def __init__(self,N): 
        self.table = [i for i in range(N)]    
        self.rank  = [1 for i in range(N)]    
        self.size  = [1 for i in range(N)]    

    def Find(self,x):    
        if self.table[x] == x:
            return x
        else:
            self.table[x] = self.Find(self.table[x])    
            self.size[x] = self.size[self.table[x]]
            return self.table[x]

    def Unite(self,x,y):    
        x,y = self.Find(x), self.Find(y)
        sx,sy = self.Size(x), self.Size(y)
        if x == y: return
        if self.rank[x] > self.rank[y]:
            self.table[y] = x
            self.size[x] = sx + sy
        else:
            self.table[x] = y
            self.size[y] = sx + sy
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def Check(self,x,y):
        return self.Find(x) == self.Find(y)

    def Size(self,x):
        return self.size[self.Find(x)]



N = int(input())
UF = UnionFind(N)

xys = [inpl() for _ in range(N)]
xys.sort()
tatedict = defaultdict(list)
xx = set()
for x,y in xys:
    tatedict[x].append(y)
    xx.add(x)

for x in xx:
    s = tatedict[x][0]
    for t in tatedict[x]:
        if not UF.Check(s,t):
            UF.Unite(s,t)

ans = 0
for x in xx:
    s = tatedict[x][0]
    ans += UF.Size(s) - len(tatedict[x])

print(ans)


from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())


class PotentialUnionFind:
    def __init__(self,N): 
        self.table = [i for i in range(N)]    
        self.rank  = [1 for i in range(N)]    
        self.size  = [1 for i in range(N)]    
        self.diffweight = [0 for i in range(N)]

    def Find(self,x):    
        if self.table[x] == x:
            return x
        else:
            root = self.Find(self.table[x]) 
            self.size[x] = self.size[self.table[x]]
            self.diffweight[x] += self.diffweight[self.table[x]]
            self.table[x] = root
            return root

    def Unite(self,x,y,w): 
        w = w - self.Weight(y) + self.Weight(x)
        x,y = self.Find(x), self.Find(y)
        sx,sy = self.Size(x), self.Size(y)
        if x == y: return
        if self.rank[x] > self.rank[y]:
            self.table[y] = x
            self.size[x] = sx + sy
            self.diffweight[y] = w
        else:
            self.table[x] = y
            self.size[y] = sx + sy
            self.diffweight[x] = -w
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def Check(self,x,y):
        return self.Find(x) == self.Find(y)

    def Size(self,x):
        return self.size[self.Find(x)]

    def Weight(self,x): 
        self.Find(x)
        return self.diffweight[x]

    def Diff(self,x,y): 
        return self.Weight(y) - self.Weight(x)


N = int(input())
PUF = PotentialUnionFind(10**5+1)

xys = [inpl() for _ in range(N)]
xys.sort()
tatedict = defaultdict(list)
xx = set()
for x,y in xys:
    tatedict[x].append(y)
    xx.add(x)

for x in xx:
    s = tatedict[x][0]
    for t in tatedict[x]:
        if not PUF.Check(s,t):
            PUF.Unite(s,t,1)

ans = 0
for x in xx:
    s = tatedict[x][0]
    ans += PUF.Size(s) - len(tatedict[x])

print(ans)


Verdicts: Runtime Error

Accepted