@@ -44,13 +44,13 @@ Output: [4,1]
44
44
- ` UnionFind ` algorithm typically has three methods:
45
45
- The ` unite(node1, node2) ` operation is used to merge two trees.
46
46
- 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.
48
48
49
49
## Approach
50
50
1 . Iterate ` edges ` data to look for the ` two_conflict_edges ` (the two edges caused a vertex with in-degree 2).
51
51
1 . Initially, each node is in its own group.
52
52
1 . 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] ` .
54
54
1 . If there is a vertex with in-degree 2, we need to determine which edge in ` two_conflict_edges ` should be returned.
55
55
See 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.
56
56
@@ -61,60 +61,65 @@ See if the graph can form a cycle by not adding the second edge to the graph. If
61
61
## Python
62
62
``` python
63
63
class Solution :
64
- def __init__ (self ):
65
- self .parent = None
66
-
67
64
def 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 ))
71
66
72
- if not conflict_edges:
67
+ two_conflict_edges_ = self .two_conflict_edges(edges)
68
+
69
+ if not two_conflict_edges_:
73
70
for x, y in edges:
74
- if self .same_root (x, y):
71
+ if self .is_same_root (x, y):
75
72
return [x, y]
76
73
77
74
self .unite(x, y)
78
75
79
- raise Exception (' No suitable edge was returned' )
76
+ raise Exception (' No suitable edge was returned! ' )
80
77
81
78
for x, y in edges:
82
- if [x, y] == conflict_edges [1 ]:
79
+ if [x, y] == two_conflict_edges_ [1 ]:
83
80
continue
84
81
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 ]
87
84
88
85
self .unite(x, y)
89
-
90
- return conflict_edges[1 ]
91
86
92
- def unite (self , x , y ):
93
- self .parent[y] = x
87
+ return two_conflict_edges_[1 ]
94
88
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 = {}
98
91
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_node in edges:
93
+ if pointed_node in pointed_node_to_source_node:
94
+ return [
95
+ [pointed_node_to_source_node[pointed_node], pointed_node],
96
+ [source_node, pointed_node],
97
+ ]
103
98
99
+ pointed_node_to_source_node[pointed_node] = source_node
104
100
105
- def two_conflict_edges (edges ):
106
- conflict_edges = []
107
- child_to_parent = {}
101
+ return []
108
102
109
- for parent, child in edges:
110
- if child in 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]
114
111
115
- child_to_parent[child] = parent
112
+ if x == parent:
113
+ return x
114
+
115
+ root = self .find_root(parent)
116
116
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)
118
123
```
119
124
120
125
## Java
0 commit comments