1
+ from collections import defaultdict , deque , Counter
2
+ # d = deque()
3
+ # d.append(5)
4
+ # x = d.popleft()
5
+ import re
6
+ # m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
7
+ # # or re.search
8
+ # >>> m.group(0) # The entire match
9
+ # 'Isaac Newton'
10
+ # >>> m.group(1) # The first parenthesized subgroup.
11
+ # 'Isaac'
12
+ # >>> m.group(2) # The second parenthesized subgroup.
13
+ # 'Newton'
14
+ # >>> m.group(1, 2) # Multiple arguments give us a tuple.
15
+ # ('Isaac', 'Newton')
16
+ from heapq import heappush , heappop
17
+ # >>> heap = []
18
+ # >>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
19
+ # >>> for item in data:
20
+ # ... heappush(heap, item)
21
+ # heap[0] is the smallest item
22
+ import string
23
+ # string.ascii_lowercase == 'abcde...'
24
+ # string.ascii_uppercase == 'ABCDE...'
25
+ from functools import lru_cache
26
+ # @lru_cache(maxsize=None)
27
+ import numpy as np
28
+
29
+ import sys
30
+
31
+ sys .setrecursionlimit (100000 )
32
+
33
+ def get_ints (s ):
34
+ return list (map (int , re .findall (r"-?\d+" , s ))) # copied from mcpower from mserrano on betaveros' recommendation
35
+ dirs = [(0 ,1 ), (1 ,0 ), (0 ,- 1 ), (- 1 ,0 )]
36
+ directions = 'RDLU'
37
+ octs = [(0 ,1 ),(1 ,1 ),(1 ,0 ),(1 ,- 1 ),(0 ,- 1 ),(- 1 ,- 1 ),(- 1 ,0 ),(- 1 ,1 )]
38
+ def is_grid_valid (n ,m , r ,c ,):
39
+ return (0 <= r < n ) and (0 <= c < m )
40
+ def sign_of (x ):
41
+ if x == 0 :
42
+ return 0
43
+ return x / abs (x )
44
+
45
+ INTERVAL_TYPE_INCLUSIVE = 0
46
+ INTERVAL_TYPE_EXCLUSIVE = 1
47
+ # def make_interval_class(start_type=INTERVAL_TYPE_INCLUSIVE, end_type=INTERVAL_TYPE_EXCLUSIVE):
48
+ # class Interval:
49
+ # start_type = start_type
50
+ # end_type = end_type
51
+ # def __init__(self, start, end):
52
+ # self.start = start
53
+ # self.end = end
54
+ # def merge(interval_a, interval_b):
55
+ # interval = (min(interval_a[0], interval_b[0]), max(interval_a[1], interval_b[1]))
56
+ # if interval[0] > interval[1]:
57
+ # return None
58
+ ####################################
59
+
60
+ PART = 1
61
+ # PART = 2
62
+ if PART == 1 :
63
+ ans = 0
64
+ inps = []
65
+ while True :
66
+ try :
67
+ inps .append (input ())
68
+ except EOFError :
69
+ break
70
+ R , C = len (inps ), len (inps [0 ])
71
+ edges = defaultdict (list )
72
+ for inp in inps :
73
+ x = inp .split (':' )[0 ]
74
+ ys = inp .split ()[1 :]
75
+ for y in ys :
76
+ edges [x ].append (y )
77
+ edges [y ].append (x )
78
+ import random
79
+
80
+ def bfs (start , end , rm_edges ):
81
+ q = deque ()
82
+ q .append (start )
83
+ been = {start }
84
+ prev = dict ()
85
+ while q :
86
+ u = q .popleft ()
87
+ if u == end :
88
+ break
89
+ for v in edges [u ]:
90
+ if v not in been and ((u , v ) not in rm_edges ) and ((v , u ) not in rm_edges ):
91
+ been .add (v )
92
+ prev [v ] = u
93
+ q .append (v )
94
+ if end not in been :
95
+ return []
96
+ path = []
97
+ u = end
98
+ while u != start :
99
+ v = prev [u ]
100
+ path .append (u )
101
+ u = v
102
+ return path
103
+
104
+ def dfs2 (start , rm_edges , been ):
105
+ y = 1
106
+ for x in edges [start ]:
107
+ if (start , x ) in rm_edges or (x , start ) in rm_edges :
108
+ continue
109
+ if x not in been :
110
+ been .add (x )
111
+ y += dfs2 (x , rm_edges , been )
112
+ return y
113
+
114
+ while True :
115
+ rm_edges = []
116
+ u , v = random .sample (list (edges .keys ()), 2 )
117
+ if u == v :
118
+ continue
119
+ if u in edges [v ]:
120
+ continue
121
+ while len (rm_edges ) < 3 :
122
+ path = bfs (u , v , rm_edges )
123
+ # print(rm_edges, path)
124
+ r = random .randint (1 , len (path )- 1 )
125
+ if (path [r - 1 ], path [r ]) in rm_edges or (path [r ], path [r - 1 ]) in rm_edges :
126
+ continue
127
+ rm_edges .append ( (path [r - 1 ], path [r ]))
128
+ sz = dfs2 (u , rm_edges , {u })
129
+ if sz != len (edges ):
130
+ print (sz , len (edges ))
131
+ print (sz * (len (edges ) - sz ))
132
+ break
133
+
134
+ # goddammit... I didn't have a min-cut python ready to go in python, so I tried to ask github copilot and chatgpt to write one for me
135
+ # both of which failed terribly. Eventually just went with this BFS approach instead, which worked. But way too late to get on the leaderboard :(
136
+ # and unfortunately that means I missed the overall leaderboard for this year :'(
137
+ # I actually originally tried this approach immediately, but with a DFS instead of BFS, and it did not work :(
138
+ else :
139
+ pass
0 commit comments