Lang:Python2
Edit12345678910111213141516171819202122232425262728293031r = range(10**6)def f(x):global rif r[x] == x:return xr[x] = f(r[x])return r[x]v = [False for i in r]ret = 0for i in xrange(input()):x, y = map(int, raw_input().split())u = x * 1000 + yif v[u]:continuev[u] = Trueret += 1s = set()for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:xx = x + dxyy = y + dyif xx < 0 or xx >= 1000 or yy < 0 or yy >= 1000:continuew = xx * 1000 + yyif not v[w]:continueW = f(w)s.add(W)ret -= len(s)for W in s:r[W] = uprint ret