結果
問題 | No.317 辺の追加 |
ユーザー |
![]() |
提出日時 | 2021-09-23 00:57:17 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 453 ms / 2,000 ms |
コード長 | 1,560 bytes |
コンパイル時間 | 543 ms |
コンパイル使用メモリ | 82,196 KB |
実行使用メモリ | 92,928 KB |
最終ジャッジ日時 | 2024-07-05 09:22:16 |
合計ジャッジ時間 | 11,669 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 38 |
ソースコード
from collections import Counterimport sysinput = sys.stdin.buffer.readlinesys.setrecursionlimit(10 ** 7)inf = 10 ** 9class UF_tree:def __init__(self, n):self.root = [-1] * (n + 1)self.rank = [0] * (n + 1)def find(self, x):stack = []while self.root[x] >= 0:stack.append(x)x = self.root[x]for i in stack:self.root[i] = xreturn xdef same(self, x, y):return self.find(x) == self.find(y)def unite(self, x, y):x = self.find(x)y = self.find(y)if x == y:return Falseif self.rank[x] < self.rank[y]:self.root[y] += self.root[x]self.root[x] = yelse:self.root[x] += self.root[y]self.root[y] = xif self.rank[x] == self.rank[y]:self.rank[x] += 1return Truedef size(self, x):return -self.root[self.find(x)]N, M = map(int, input().split())uf = UF_tree(N)for _ in range(M):a, b = map(int, input().split())uf.unite(a-1, b-1)leader = {uf.find(i) for i in range(N)}cnt = [uf.size(i) for i in leader]dp = [inf] * (N + 1)dp[0] = 0for W, total in Counter(cnt).items():k = 1while total > 0:mul = min(k, total)for i in reversed(range(W * mul, N+1)):dp[i] = min(dp[i], dp[i - W * mul] + mul)total -= mulk *= 2for i in range(1, N+1):if dp[i] > N + 5:print(-1)else:print(dp[i] - 1)