Lang:Python2
Edit12345678910111213141516171819202122232425262728293031N = 1000d = (-1, 0, 1, 0, 0, -1, 0, 1)res = 0n = int(raw_input())fa = [i for i in xrange(N * N + 1)]mark = [0] * (N * N + 1)def findFa(x):s = []s.append(x)while s[-1] != fa[s[-1]]: s.append(fa[s[-1]])f = s[-1]for x in s: fa[x] = freturn fwhile n:x, y = map(int, raw_input().split())u = x * N + yres += 1mark[u] = 1for i in xrange(4):xx = x + d[i * 2]yy = y + d[i * 2 + 1]if xx >= 0 and xx < N and yy >= 0 and yy < N:v = findFa(xx * N + yy)u = findFa(u)if mark[v] and u != v:fa[v] = ures -= 1print resn -= 1