@@ -44,13 +44,13 @@ Output: [4,1]
4444- ` UnionFind ` algorithm typically has three methods:
4545- The` unite(node1, node2) ` operation is used to merge two trees.
4646- The` find_root(node) ` method is used to return the root of a node.
47- - The` same_root (node1, node2)` method is used to determine whether two nodes are in the same tree.
47+ - The` is_same_root (node1, node2)` method is used to determine whether two nodes are in the same tree.
4848
4949##Approach
50501 . Iterate` edges ` data to look for the` two_conflict_edges ` (the two edges caused a vertex with in-degree 2).
51511 . Initially, each node is in its own group.
52521 . Iterate` edges ` data and` unite(node1, node2) ` .
53- 1 . If there is no vertex with in-degree 2, as soon as` same_root (node1, node2) == true` (a cycle will be formed), return` [node1, node2] ` .
53+ 1 . If there is no vertex with in-degree 2, as soon as` is_same_root (node1, node2) == true` (a cycle will be formed), return` [node1, node2] ` .
54541 . If there is a vertex with in-degree 2, we need to determine which edge in` two_conflict_edges ` should be returned.
5555See if the graph can form a cycle by not adding the second edge to the graph. If so, return the first edge. Otherwise, return the second edge.
5656
@@ -61,60 +61,65 @@ See if the graph can form a cycle by not adding the second edge to the graph. If
6161##Python
6262``` python
6363class Solution :
64- def __init__ (self ):
65- self .parent= None
66-
6764def findRedundantDirectedConnection (self ,edges : List[List[int ]]) -> List[int ]:
68- self .parent= list (range (len (edges)+ 1 ))
69-
70- conflict_edges= two_conflict_edges(edges)
65+ self .parents= list (range (len (edges)+ 1 ))
7166
72- if not conflict_edges:
67+ two_conflict_edges_= self .two_conflict_edges(edges)
68+
69+ if not two_conflict_edges_:
7370for x, yin edges:
74- if self .same_root (x, y):
71+ if self .is_same_root (x, y):
7572return [x, y]
7673
7774self .unite(x, y)
7875
79- raise Exception (' No suitable edge was returned' )
76+ raise Exception (' No suitable edge was returned! ' )
8077
8178for x, yin edges:
82- if [x, y]== conflict_edges [1 ]:
79+ if [x, y]== two_conflict_edges_ [1 ]:
8380continue
8481
85- if self .same_root (x, y):
86- return conflict_edges [0 ]
82+ if self .is_same_root (x, y):
83+ return two_conflict_edges_ [0 ]
8784
8885self .unite(x, y)
89-
90- return conflict_edges[1 ]
9186
92- def unite (self ,x ,y ):
93- self .parent[y]= x
87+ return two_conflict_edges_[1 ]
9488
95- def find_root (self ,node ):
96- if self .parent[node]== node:
97- return node
89+ def two_conflict_edges (self ,edges ):
90+ pointed_node_to_source_node= {}
9891
99- return self .find_root(self .parent[node])
100-
101- def same_root (self ,x ,y ):
102- return self .find_root(x)== self .find_root(y)
92+ for source_node, pointed_nodein edges:
93+ if pointed_nodein pointed_node_to_source_node:
94+ return [
95+ [pointed_node_to_source_node[pointed_node], pointed_node],
96+ [source_node, pointed_node],
97+ ]
10398
99+ pointed_node_to_source_node[pointed_node]= source_node
104100
105- def two_conflict_edges (edges ):
106- conflict_edges= []
107- child_to_parent= {}
101+ return []
108102
109- for parent, childin edges:
110- if childin child_to_parent:
111- conflict_edges.append([child_to_parent[child], child])
112- conflict_edges.append([parent, child])
113- break
103+ def unite (self ,x ,y ):
104+ root_x= self .find_root(x)
105+ root_y= self .find_root(y)
106+
107+ self .parents[root_y]= root_x
108+
109+ def find_root (self ,x ):
110+ parent= self .parents[x]
114111
115- child_to_parent[child]= parent
112+ if x== parent:
113+ return x
114+
115+ root= self .find_root(parent)
116116
117- return conflict_edges
117+ self .parents[x]= root
118+
119+ return root
120+
121+ def is_same_root (self ,x ,y ):
122+ return self .find_root(x)== self .find_root(y)
118123```
119124
120125##Java