結果
問題 | No.2418 情報通だよ!Nafmoくん |
ユーザー | prd_xxx |
提出日時 | 2023-08-12 13:42:45 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 451 ms / 2,000 ms |
コード長 | 1,018 bytes |
コンパイル時間 | 193 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 37,980 KB |
最終ジャッジ日時 | 2024-04-30 03:53:56 |
合計ジャッジ時間 | 6,713 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 31 ms
10,752 KB |
testcase_01 | AC | 31 ms
10,880 KB |
testcase_02 | AC | 30 ms
10,752 KB |
testcase_03 | AC | 451 ms
37,980 KB |
testcase_04 | AC | 161 ms
27,340 KB |
testcase_05 | AC | 251 ms
23,680 KB |
testcase_06 | AC | 329 ms
33,656 KB |
testcase_07 | AC | 266 ms
24,064 KB |
testcase_08 | AC | 380 ms
35,364 KB |
testcase_09 | AC | 357 ms
27,520 KB |
testcase_10 | AC | 451 ms
37,112 KB |
testcase_11 | AC | 384 ms
35,056 KB |
testcase_12 | AC | 273 ms
26,868 KB |
testcase_13 | AC | 136 ms
19,072 KB |
testcase_14 | AC | 240 ms
24,064 KB |
testcase_15 | AC | 200 ms
20,992 KB |
testcase_16 | AC | 413 ms
32,384 KB |
testcase_17 | AC | 116 ms
15,872 KB |
testcase_18 | AC | 278 ms
31,224 KB |
testcase_19 | AC | 141 ms
26,160 KB |
testcase_20 | AC | 214 ms
22,016 KB |
testcase_21 | AC | 163 ms
20,480 KB |
testcase_22 | AC | 140 ms
17,408 KB |
ソースコード
import sys input = sys.stdin.readline N,M = map(int,input().split()) AB = [tuple(map(int,input().split())) for i in range(M)] class UnionFind: def __init__(self,N): self.parent = [i for i in range(N)] self._size = [1] * N self.count = 0 def root(self,a): if self.parent[a] == a: return a else: self.parent[a] = self.root(self.parent[a]) return self.parent[a] def is_same(self,a,b): return self.root(a) == self.root(b) def unite(self,a,b): ra = self.root(a) rb = self.root(b) if ra == rb: return if self._size[ra] < self._size[rb]: ra,rb = rb,ra self._size[ra] += self._size[rb] self.parent[rb] = ra self.count += 1 def size(self,a): return self._size[self.root(a)] uf = UnionFind(2*N) for a,b in AB: a,b = a-1,b-1 uf.unite(a,b) rs = set() for i in range(2*N): rs.add(uf.root(i)) ans = N for r in rs: ans -= uf.size(r)//2 print(ans)