File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -178,6 +178,45 @@ int main() {
178
178
179
179
### Python
180
180
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
+
181
220
### Go
182
221
183
222
### Rust
You can’t perform that action at this time.
0 commit comments