summaryrefslogtreecommitdiff
path: root/kattis-open/almostunionfind/bad.py
diff options
context:
space:
mode:
authormathiasmagnusson <mathiasmagnussons@gmail.com>2022-12-09 18:00:41 +0100
committermathiasmagnusson <mathiasmagnussons@gmail.com>2022-12-09 18:00:41 +0100
commita1eb38bebe6ce1668c3f96489506c3b05b9fe5cb (patch)
treecccf0fd4763dba123efab7c896292dc18d1eb458 /kattis-open/almostunionfind/bad.py
parente41e6c8bc72e3300a0fa137f198454341bc315b1 (diff)
downloadprogramming-problem-solving-a1eb38bebe6ce1668c3f96489506c3b05b9fe5cb.tar.gz
Move stuff around
Diffstat (limited to 'kattis-open/almostunionfind/bad.py')
-rwxr-xr-xkattis-open/almostunionfind/bad.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/kattis-open/almostunionfind/bad.py b/kattis-open/almostunionfind/bad.py
new file mode 100755
index 0000000..1d4cbec
--- /dev/null
+++ b/kattis-open/almostunionfind/bad.py
@@ -0,0 +1,53 @@
+#!/usr/bin/python
+
+while True:
+ try:
+ line = input()
+ except EOFError:
+ break
+ n, m = map(int, line.split())
+
+ u = [[i + 1] for i in range(n)]
+
+ for _ in range(m):
+ line = input().split()
+ p = int(line[1])
+ if line[0] != '3':
+ q = int(line[2])
+
+ if line[0] == '1': # union
+ a, b = False, False
+ for s in u:
+ if p in s:
+ a = s
+ if q in s:
+ b = s
+ if a == False or b == False:
+ raise ValueError(f"could not find {p} or {q} in {u}")
+ if a != b:
+ for i in b:
+ a.append(i)
+ b.clear()
+ u.remove(b)
+ elif line[0] == '2': # move
+ a, b = False, False
+ for s in u:
+ if p in s:
+ a = s
+ if q in s:
+ b = s
+ if a == False or b == False:
+ raise ValueError(f"could not find {p} or {q} in {u}")
+ if a != b:
+ a.remove(p)
+ b.append(p)
+ if not a:
+ u.remove(a)
+ elif line[0] == '3':
+ for s in u:
+ if p in s:
+ print(len(s), sum(s))
+ break
+ else:
+ raise ValueError(f"could not find {p} in {u}")
+ # print(u)