結果
問題 | No.317 辺の追加 |
ユーザー |
![]() |
提出日時 | 2015-12-10 17:31:08 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,045 bytes |
コンパイル時間 | 169 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 87,552 KB |
最終ジャッジ日時 | 2024-09-15 07:38:35 |
合計ジャッジ時間 | 4,318 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | TLE * 1 -- * 37 |
ソースコード
from collections import Counterclass UnionFind:def __init__(self, n):self.par = list(range(n))def find(self, x):if self.par[x] == x:return xelse:self.par[x] = self.find(self.par[x])return self.par[x]def union(self, x, y):x = self.find(x)y = self.find(y)if x == y:returnself.par[x] = ydef solve():N, M = map(int, input().split())uf = UnionFind(N)for i in range(M):u, v = map(int, input().split())uf.union(u - 1, v - 1)for i in range(N):uf.find(i)co = Counter(uf.par)gs = []for k in co:gs.append(co[k])inf = float('inf')dp = [inf] * (N + 1)dp[0] = -1for g in gs:for i in range(N, -1, -1):if dp[i] != inf and i + g <= N:dp[i + g] = min(dp[i + g], dp[i] + 1)for i in range(1, N + 1):print(dp[i] if dp[i] < inf else -1)if __name__ == '__main__':solve()