結果
| 問題 |
No.2418 情報通だよ!Nafmoくん
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-20 21:14:41 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 202 ms / 2,000 ms |
| コード長 | 1,250 bytes |
| コンパイル時間 | 140 ms |
| コンパイル使用メモリ | 82,376 KB |
| 実行使用メモリ | 107,168 KB |
| 最終ジャッジ日時 | 2025-03-20 21:15:19 |
| 合計ジャッジ時間 | 3,829 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 21 |
ソースコード
class UnionFind:
def __init__(self, size):
self.parent = list(range(size + 1)) # 1-based indexing
self.size = [1] * (size + 1)
def find(self, x):
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def unite(self, x, y):
x_root = self.find(x)
y_root = self.find(y)
if x_root == y_root:
return
if self.size[x_root] < self.size[y_root]:
x_root, y_root = y_root, x_root
self.parent[y_root] = x_root
self.size[x_root] += self.size[y_root]
def main():
import sys
input = sys.stdin.read().split()
idx = 0
N = int(input[idx])
idx += 1
M = int(input[idx])
idx += 1
students = 2 * N
uf = UnionFind(students)
for _ in range(M):
a = int(input[idx])
idx += 1
b = int(input[idx])
idx += 1
uf.unite(a, b)
seen = set()
total_remainder = 0
for i in range(1, students + 1):
root = uf.find(i)
if root not in seen:
seen.add(root)
s = uf.size[root]
total_remainder += s % 2
print(total_remainder // 2)
if __name__ == "__main__":
main()
lam6er