forked from pyramidsnail/bioinformatics-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.py
57 lines (42 loc) · 1009 Bytes
/
tree.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
import sys, re, os
f = open('test','rU')
lines = f.readlines()
n = int(lines[0].strip())
parent={}
for i in xrange(1,n+1):
parent[i] = i
def union(u,v):
r1 = find(u)
r2 = find(v)
parent[r2] = r1
def find(u):
while parent[u] != u:
u = parent[u]
return u
for i in lines[1:]:
start = int(i.strip().split()[0])
end = int(i.strip().split()[1])
union(start, end)
count = 0
for i in parent:
if i == parent[i]:
count += 1
print count-1
# contain = []
# parts = []
# for i in lines[1:]:
# start = i.strip().split()[0]
# end = i.strip().split()[1]
# if not start in contain:
# contain.append(start)
# if not end in contain:
# contain.append(end)
# flag = 0
# for x in parts:
# if start in x or end in x:
# x.extend([start, end])
# flag = 1
# break
# if not flag:
# parts.append([start, end])
# print int(lines[0].strip())-len(contain)+len(parts)-1