結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー Seed57_cashSeed57_cash
提出日時 2023-06-21 09:51:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 275 ms / 2,000 ms
コード長 1,366 bytes
コンパイル時間 157 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 89,420 KB
最終ジャッジ日時 2024-04-30 02:51:18
合計ジャッジ時間 4,995 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
51,840 KB
testcase_01 AC 39 ms
51,840 KB
testcase_02 AC 49 ms
52,096 KB
testcase_03 AC 217 ms
88,704 KB
testcase_04 AC 107 ms
80,384 KB
testcase_05 AC 240 ms
85,036 KB
testcase_06 AC 180 ms
84,992 KB
testcase_07 AC 237 ms
85,484 KB
testcase_08 AC 173 ms
85,888 KB
testcase_09 AC 266 ms
87,488 KB
testcase_10 AC 243 ms
88,960 KB
testcase_11 AC 204 ms
87,680 KB
testcase_12 AC 177 ms
84,224 KB
testcase_13 AC 180 ms
80,588 KB
testcase_14 AC 194 ms
83,584 KB
testcase_15 AC 163 ms
83,200 KB
testcase_16 AC 275 ms
89,420 KB
testcase_17 AC 148 ms
79,744 KB
testcase_18 AC 162 ms
84,352 KB
testcase_19 AC 98 ms
79,616 KB
testcase_20 AC 169 ms
83,712 KB
testcase_21 AC 162 ms
81,408 KB
testcase_22 AC 188 ms
81,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 要するに、知り合いをグラフにしたときに、同じ連結成分<->知り合い
# ということで連結成分の中で組めるだけペアを組めばいい


class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n + 1)]
        self.rank = [0] * (n + 1)

    # search
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # unite
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # check
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


n, m = map(int, input().split())
ab_list = [tuple(map(int, input().split())) for _ in range(m)]

# 連結成分に分解する
# unionfind
uf = UnionFind(2 * n)
for a, b in ab_list:
	uf.union(a, b)

# 親番号(=連結成分番号)をカウントする
p_count = [0] * (2 * n + 1)
for i in range(2 * n):
	p = uf.find(i + 1)
	p_count[p] += 1

# 連結成分ごとに、その中で組める知り合い同士の組の数を求める
res = 0
for i in range(2 * n):
	res += p_count[i + 1] // 2

print(n - res)
0