-
Notifications
You must be signed in to change notification settings - Fork 4
/
articulation_point_network_315.py
62 lines (44 loc) · 1.3 KB
/
articulation_point_network_315.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
#from collections import defaultdict
result = set()
def ap_find(graph, u, visited, d, low, time, parent, start):
global result
time = time+1
d[u] = time
low[u] = time
visited[u] = True
no_of_childs = 0
for v in graph[u]:
if(v==parent[u]):
continue
elif(visited[v]):
low[u] = min(low[u], d[v])
elif(not visited[v]):
parent[v] = u
ap_find(graph, v, visited, d, low, time, parent, start)
low[u] = min(low[u], low[v])
if(low[v]>=d[u] and u!=start):
result.add(u)
no_of_childs+=1
if(no_of_childs>1 and u==start):
result.add(u)
if __name__=="__main__":
while True:
n = int(input())
if(n==0):
break
graph = {i:[] for i in range(1,n+1)}
while True:
l = list(map(int, input().split()))
if(len(l)==1):
break
for i in range(1, len(l)):
graph[l[0]].append(l[i])
graph[l[i]].append(l[0])
visited = [False]*(n+1)
d = [0]*(n+1)
low = [0]*(n+1)
time = 0
parent = [-1]*(n+1)
ap_find(graph, 1, visited, d, low, time, parent, 1)
print(len(result))
result = set()