Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd0528cd

Browse files
committed
685-redundant-connection-ii.md Perfected code.
1 parente5f8b59 commitd0528cd

File tree

2 files changed

+80
-70
lines changed

2 files changed

+80
-70
lines changed

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

Lines changed: 40 additions & 35 deletions
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
classSolution:
64-
def__init__(self):
65-
self.parent=None
66-
6764
deffindRedundantDirectedConnection(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-
ifnot conflict_edges:
67+
two_conflict_edges_=self.two_conflict_edges(edges)
68+
69+
ifnot two_conflict_edges_:
7370
for x, yin edges:
74-
ifself.same_root(x, y):
71+
ifself.is_same_root(x, y):
7572
return [x, y]
7673

7774
self.unite(x, y)
7875

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

8178
for x, yin edges:
82-
if [x, y]==conflict_edges[1]:
79+
if [x, y]==two_conflict_edges_[1]:
8380
continue
8481

85-
ifself.same_root(x, y):
86-
returnconflict_edges[0]
82+
ifself.is_same_root(x, y):
83+
returntwo_conflict_edges_[0]
8784

8885
self.unite(x, y)
89-
90-
return conflict_edges[1]
9186

92-
defunite(self,x,y):
93-
self.parent[y]= x
87+
return two_conflict_edges_[1]
9488

95-
deffind_root(self,node):
96-
ifself.parent[node]== node:
97-
return node
89+
deftwo_conflict_edges(self,edges):
90+
pointed_node_to_source_node= {}
9891

99-
returnself.find_root(self.parent[node])
100-
101-
defsame_root(self,x,y):
102-
returnself.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-
deftwo_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+
defunite(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+
deffind_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+
defis_same_root(self,x,y):
122+
returnself.find_root(x)==self.find_root(y)
118123
```
119124

120125
##Java

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

Lines changed: 40 additions & 35 deletions
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
classSolution:
64-
def__init__(self):
65-
self.parent=None
66-
6764
deffindRedundantDirectedConnection(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-
ifnot conflict_edges:
67+
two_conflict_edges_=self.two_conflict_edges(edges)
68+
69+
ifnot two_conflict_edges_:
7370
for x, yin edges:
74-
ifself.same_root(x, y):
71+
ifself.is_same_root(x, y):
7572
return [x, y]
7673

7774
self.unite(x, y)
7875

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

8178
for x, yin edges:
82-
if [x, y]==conflict_edges[1]:
79+
if [x, y]==two_conflict_edges_[1]:
8380
continue
8481

85-
ifself.same_root(x, y):
86-
returnconflict_edges[0]
82+
ifself.is_same_root(x, y):
83+
returntwo_conflict_edges_[0]
8784

8885
self.unite(x, y)
89-
90-
return conflict_edges[1]
9186

92-
defunite(self,x,y):
93-
self.parent[y]= x
87+
return two_conflict_edges_[1]
9488

95-
deffind_root(self,node):
96-
ifself.parent[node]== node:
97-
return node
89+
deftwo_conflict_edges(self,edges):
90+
pointed_node_to_source_node= {}
9891

99-
returnself.find_root(self.parent[node])
100-
101-
defsame_root(self,x,y):
102-
returnself.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-
deftwo_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+
defunite(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+
deffind_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+
defis_same_root(self,x,y):
122+
returnself.find_root(x)==self.find_root(y)
118123
```
119124

120125
##Java

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp