-
Notifications
You must be signed in to change notification settings - Fork 0
/
14834Foxlings.py
61 lines (48 loc) · 1.28 KB
/
14834Foxlings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Practice disjoint set
# http://www.spoj.com/problems/FOXLINGS/
class DisjointSet():
def __init__(self, n):
self.root = {}
self.num = n # number of disjoint sets
self.sz = {}
def findRoot(self, val):
if val not in self.root:
self.root[val] = val
self.sz[val] = 1
return val
while val != self.root[val]:
self.root[val] = self.root[self.root[val]]
val = self.root[val]
return val
def union(self, val1, val2):
val1 = self.findRoot(val1)
val2 = self.findRoot(val2)
if val1 == val2:
return
if self.sz[val1] > self.sz[val2]:
self.root[val2] = val1
self.sz[val1] += self.sz[val2]
else:
self.root[val1] = val2
self.sz[val2] += self.sz[val1]
self.num -= 1
def getNumOfDisjointSet(self):
return self.num
def main():
#g = sys.stdin
g = open("FOXLINGS", "r")
s = g.readline()
l1 = s.split( )
num_nodes = int(l1[0])
num_edges = int(l1[1])
djSet = DisjointSet(num_nodes)
# for line in g:
# edge = line.split()
# djSet.union(int(edge[0]) - 1, int(edge[1]) - 1)
for _ in range(num_edges):
line = g.readline()
edge = line.split()
djSet.union(int(edge[0]) - 1, int(edge[1]) - 1)
print(djSet.getNumOfDisjointSet())
if __name__ == '__main__':
main()