Created
April 26, 2023 10:55
-
-
Save modos/9ea29f37122bd00d87641291c69bd4bf to your computer and use it in GitHub Desktop.
مسافرت
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#In the name of God | |
import sys | |
sys.setrecursionlimit(1000 * 100 + 10) | |
def dfs(s): | |
mark[s] = True | |
for i in range(0, n): | |
if x[s] == x[i] or y[s] == y[i]: | |
if not mark[i]: | |
dfs(i) | |
return | |
n = int(input()) | |
mark = [False] * n | |
x = [None] * n | |
y = [None] * n | |
adj = [[False for i in range(0, n)] for i in range(0, n)] | |
for i in range(0, n): | |
x[i], y[i] = map(int, input().split()) | |
ans = 0 | |
for i in range(0, n): | |
if not mark[i]: | |
ans += 1 | |
dfs(i) | |
print(int(ans - 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment