File tree 2 files changed +43
-0
lines changed
2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ [flake8]
2
+ ignore =
3
+ # indentation is not a multiple of four,
4
+ E111,E114,
5
+ # visually indented line with same indent as next logical line,
6
+ E129
7
+
8
+ max-line-length =100
Original file line number Diff line number Diff line change
1
+ import abc
2
+ from typing import List
3
+
4
+
5
+ def dynamic_connectivity_brute_force (components : List [int ]) -> 'DynamicConnectivity' :
6
+ return _BruteForce (components )
7
+
8
+
9
+ class DynamicConnectivity (metaclass = abc .ABCMeta ):
10
+ @abc .abstractmethod
11
+ def is_connected (self , a : int , b : int ) -> bool :
12
+ pass
13
+
14
+ @abc .abstractmethod
15
+ def connect (self , a : int , b : int ) -> bool :
16
+ pass
17
+
18
+
19
+ class _BruteForce (DynamicConnectivity ):
20
+ def __init__ (self , components : List [int ]):
21
+ self ._components = components
22
+
23
+ def is_connected (self , a : int , b : int ) -> bool :
24
+ return self ._components [a ] == self ._components [b ]
25
+
26
+ def connect (self , a : int , b : int ) -> bool :
27
+ a_ptr = self ._components [a ]
28
+ b_ptr = self ._components [b ]
29
+ # test if already connected
30
+ if a_ptr == b_ptr :
31
+ return True
32
+
33
+ for i , x in enumerate (self ._components ):
34
+ if self ._components [i ] == a_ptr :
35
+ self ._components [i ] = b_ptr
You can’t perform that action at this time.
0 commit comments