Skip to content

Commit d0528cd

Browse files
committed
685-redundant-connection-ii.md Perfected code.
1 parent e5f8b59 commit d0528cd

File tree

2 files changed

+80
-70
lines changed

2 files changed

+80
-70
lines changed

en/1-1000/685-redundant-connection-ii.md

+40-35
Original file line numberDiff line numberDiff line change
@@ -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
5050
1. Iterate `edges` data to look for the `two_conflict_edges` (the two edges caused a vertex with in-degree 2).
5151
1. Initially, each node is in its own group.
5252
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]`.
5454
1. If there is a vertex with in-degree 2, we need to determine which edge in `two_conflict_edges` should be returned.
5555
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.
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
6363
class Solution:
64-
def __init__(self):
65-
self.parent = None
66-
6764
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))
7166

72-
if not conflict_edges:
67+
two_conflict_edges_ = self.two_conflict_edges(edges)
68+
69+
if not two_conflict_edges_:
7370
for x, y in edges:
74-
if self.same_root(x, y):
71+
if self.is_same_root(x, y):
7572
return [x, y]
7673

7774
self.unite(x, y)
7875

79-
raise Exception('No suitable edge was returned')
76+
raise Exception('No suitable edge was returned!')
8077

8178
for x, y in edges:
82-
if [x, y] == conflict_edges[1]:
79+
if [x, y] == two_conflict_edges_[1]:
8380
continue
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

8885
self.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_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+
]
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, 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]
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

zh/1-1000/685-redundant-connection-ii.md

+40-35
Original file line numberDiff line numberDiff line change
@@ -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
5050
1. Iterate `edges` data to look for the `two_conflict_edges` (the two edges caused a vertex with in-degree 2).
5151
1. Initially, each node is in its own group.
5252
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]`.
5454
1. If there is a vertex with in-degree 2, we need to determine which edge in `two_conflict_edges` should be returned.
5555
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.
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
6363
class Solution:
64-
def __init__(self):
65-
self.parent = None
66-
6764
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))
7166

72-
if not conflict_edges:
67+
two_conflict_edges_ = self.two_conflict_edges(edges)
68+
69+
if not two_conflict_edges_:
7370
for x, y in edges:
74-
if self.same_root(x, y):
71+
if self.is_same_root(x, y):
7572
return [x, y]
7673

7774
self.unite(x, y)
7875

79-
raise Exception('No suitable edge was returned')
76+
raise Exception('No suitable edge was returned!')
8077

8178
for x, y in edges:
82-
if [x, y] == conflict_edges[1]:
79+
if [x, y] == two_conflict_edges_[1]:
8380
continue
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

8885
self.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_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+
]
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, 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]
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

0 commit comments

Comments
 (0)