結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー dmasuprodmasupro
提出日時 2023-08-12 15:09:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 304 ms / 2,000 ms
コード長 1,614 bytes
コンパイル時間 423 ms
コンパイル使用メモリ 82,580 KB
実行使用メモリ 114,720 KB
最終ジャッジ日時 2024-04-30 08:13:50
合計ジャッジ時間 5,721 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
54,632 KB
testcase_01 AC 42 ms
54,152 KB
testcase_02 AC 45 ms
55,660 KB
testcase_03 AC 264 ms
107,116 KB
testcase_04 AC 142 ms
107,364 KB
testcase_05 AC 251 ms
83,872 KB
testcase_06 AC 221 ms
107,852 KB
testcase_07 AC 241 ms
81,808 KB
testcase_08 AC 219 ms
114,720 KB
testcase_09 AC 273 ms
85,828 KB
testcase_10 AC 277 ms
102,456 KB
testcase_11 AC 242 ms
99,432 KB
testcase_12 AC 200 ms
97,064 KB
testcase_13 AC 167 ms
83,016 KB
testcase_14 AC 205 ms
90,768 KB
testcase_15 AC 130 ms
77,152 KB
testcase_16 AC 304 ms
91,284 KB
testcase_17 AC 141 ms
77,684 KB
testcase_18 AC 193 ms
101,176 KB
testcase_19 AC 128 ms
104,212 KB
testcase_20 AC 141 ms
77,028 KB
testcase_21 AC 173 ms
85,728 KB
testcase_22 AC 181 ms
78,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M = list(map(int, input().split()))



class UnionFind():
    '''
     - UnionFind
    n: 要素数
    '''
    def __init__(self, n):
        self.par = [-1] * n
        self.rank = [0] * n
        self.siz = [1] * n

    # 根を求める
    def root(self, x):
        if self.par[x] == -1: return x # x が根の場合は x を返す
        else:
          self.par[x] = self.root(self.par[x]) # 経路圧縮
          return self.par[x]

    # x と y が同じグループに属するか (根が一致するか)
    def issame(self, x, y):
        return self.root(x) == self.root(y)

    # x を含むグループと y を含むグループを併合する
    def unite(self, x, y):
        # x 側と y 側の根を取得する
        rx = self.root(x)
        ry = self.root(y)
        if rx == ry: return False # すでに同じグループのときは何もしない
        # union by rank
        if self.rank[rx] < self.rank[ry]: # ry 側の rank が小さくなるようにする
            rx, ry = ry, rx
        self.par[ry] = rx # ry を rx の子とする
        if self.rank[rx] == self.rank[ry]: # rx 側の rank を調整する
            self.rank[rx] += 1
        self.siz[rx] += self.siz[ry] # rx 側の siz を調整する
        return True


uf = UnionFind(2*N)
g = [ [] for _ in range(2*N)]
for _ in range(M):
    a,b = list(map(int, input().split()))
    
    uf.unite(a-1,b-1)
group = [-1]*(2*N)
for i in range(2*N):
    group[i] = uf.root(i)
from collections import Counter
goup_size = Counter(group)
ans = 0
for g in goup_size:
    ans += goup_size[g]%2
print(ans//2)
    
0