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

Commit 8ef0a47

Browse files
Merge pull request youngyangyang04#2749 from suinming/kamacoder-108-py
feat: 108. 冗余连接新增python解法
2 parents 0b7924f + 2841f59 commit 8ef0a47

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

‎problems/kamacoder/0108.冗余连接.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,45 @@ int main() {
178178
179179
### Python
180180
181+
```python
182+
father = list()
183+
184+
def find(u):
185+
if u == father[u]:
186+
return u
187+
else:
188+
father[u] = find(father[u])
189+
return father[u]
190+
191+
def is_same(u, v):
192+
u = find(u)
193+
v = find(v)
194+
return u == v
195+
196+
def join(u, v):
197+
u = find(u)
198+
v = find(v)
199+
if u != v:
200+
father[u] = v
201+
202+
if __name__ == "__main__":
203+
# 輸入
204+
n = int(input())
205+
for i in range(n + 1):
206+
father.append(i)
207+
# 尋找冗余邊
208+
result = None
209+
for i in range(n):
210+
s, t = map(int, input().split())
211+
if is_same(s, t):
212+
result = str(s) + ' ' + str(t)
213+
else:
214+
join(s, t)
215+
216+
# 輸出
217+
print(result)
218+
```
219+
181220
### Go
182221

183222
### Rust

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /