提出 #22078352
ソースコード 拡げる
from collections import defaultdict
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
group_members = defaultdict(list)
for member in range(self.n):
group_members[self.find(member)].append(member)
return group_members
def __str__(self):
return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())
N,M = map(int, input().split())
uf = UnionFind(N)
for i in range(M):
a,b = map(int, input().split())
uf.union(a-1,b-1)
cnt = 0
for i in range(N-1):
if not uf.same(i,i+1):
uf.union(i,i+1)
cnt+=1
print(cnt)
提出情報
| 提出日時 | |
|---|---|
| 問題 | C - Connect Cities |
| ユーザ | H20 |
| 言語 | PyPy3 (7.3.0) |
| 得点 | 300 |
| コード長 | 1591 Byte |
| 結果 | AC |
| 実行時間 | 336 ms |
| メモリ | 75804 KiB |
ジャッジ結果
| セット名 | Sample | All | ||||
|---|---|---|---|---|---|---|
| 得点 / 配点 | 0 / 0 | 300 / 300 | ||||
| 結果 |
|
|
| セット名 | テストケース |
|---|---|
| Sample | example0.txt |
| All | 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, example0.txt |
| ケース名 | 結果 | 実行時間 | メモリ |
|---|---|---|---|
| 000.txt | AC | 220 ms | 69676 KiB |
| 001.txt | AC | 184 ms | 71340 KiB |
| 002.txt | AC | 159 ms | 72656 KiB |
| 003.txt | AC | 287 ms | 73208 KiB |
| 004.txt | AC | 285 ms | 72984 KiB |
| 005.txt | AC | 231 ms | 73892 KiB |
| 006.txt | AC | 297 ms | 73736 KiB |
| 007.txt | AC | 74 ms | 68800 KiB |
| 008.txt | AC | 310 ms | 73376 KiB |
| 009.txt | AC | 218 ms | 71316 KiB |
| 010.txt | AC | 329 ms | 73372 KiB |
| 011.txt | AC | 320 ms | 73936 KiB |
| 012.txt | AC | 320 ms | 73820 KiB |
| 013.txt | AC | 336 ms | 74636 KiB |
| 014.txt | AC | 325 ms | 73528 KiB |
| 015.txt | AC | 315 ms | 72668 KiB |
| 016.txt | AC | 310 ms | 73660 KiB |
| 017.txt | AC | 322 ms | 75804 KiB |
| 018.txt | AC | 313 ms | 73864 KiB |
| 019.txt | AC | 310 ms | 74552 KiB |
| example0.txt | AC | 61 ms | 65276 KiB |