summaryrefslogtreecommitdiff
path: root/resitev.py
diff options
context:
space:
mode:
authorTom Li Dobnik <tomlidobnik1@gmail.com>2026-03-20 17:06:47 +0100
committerTom Li Dobnik <tomlidobnik1@gmail.com>2026-03-20 17:06:47 +0100
commit9f145b19dcf9f32b33caff97143a9dd02decb23b (patch)
tree7e1fe3c249f58aa6d8ad0415e2e8880e96e600c4 /resitev.py
Diffstat (limited to 'resitev.py')
-rw-r--r--resitev.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/resitev.py b/resitev.py
new file mode 100644
index 0000000..5f19624
--- /dev/null
+++ b/resitev.py
@@ -0,0 +1,24 @@
+import sys
+
+
+def find_parent(index, parents):
+ if index != parents[index]:
+ parents[index] = find_parent(parents[index], parents)
+ return parents[index]
+
+
+def main():
+ with open(sys.argv[1]) as f:
+ while True:
+ N, M = map(int, f.readline().split())
+ if N == 0 and M == 0:
+ break
+ parents = list(range(N + 1))
+ for _ in range(M):
+ i, j = map(int, f.readline().split())
+ parents[find_parent(i, parents)] = find_parent(j, parents)
+ print(len(set(find_parent(x, parents) for x in range(1, N + 1))))
+
+
+if __name__ == "__main__":
+ main()