Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- when disc[u]<low[v]-------------------------->(1)
- add (u,v) to bridge list
- In case of articulation point we use condition disc[u]<=low[v] and root children condition.
- Here we use only one condition and remember there is no equal condition in (1) which differs from articulation points
- '''
- from collections import defaultdict as dd
- class Solution:
- def criticalConnections(self, n: int, cons: List[List[int]]) -> List[List[int]]:
- adj=dd(set)
- for u,v in cons:
- adj[u].add(v)
- adj[v].add(u)
- parent,disc,low,time,ans=[-1]*n,[-1]*n,[0]*n,0,[]
- def dfs(u):
- nonlocal time
- low[u]=disc[u]=time
- time+=1
- for v in adj[u]:
- if v==parent[u]:continue#ignore parent
- if disc[v]==-1:#if not visited
- parent[v]=u
- dfs(v)
- low[u]=min(low[u],low[v])
- if disc[u]<low[v]:#----------------------->VVIMP condition
- ans.append((u,v))
- else:
- low[u]=min(low[u],disc[v])#--------------->for visited nodes same as tarjan algo+articulation points
- for i in range(n):
- if disc[i]==-1:
- dfs(i)
- return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement