結果
問題 | No.317 辺の追加 |
ユーザー |
|
提出日時 | 2017-11-14 09:26:54 |
言語 | D (dmd 2.109.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,534 bytes |
コンパイル時間 | 754 ms |
コンパイル使用メモリ | 118,592 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-06-12 22:31:45 |
合計ジャッジ時間 | 6,882 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 13 WA * 25 |
ソースコード
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; immutable int INF = 1 << 29; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto uf = new UnionFind(N); foreach (_; 0..M) { s = readln.split.map!(to!int); uf.unite(s[0]-1, s[1]-1); } int[int] cnt; foreach (i; 0..N) if (uf.table[i] < 0) cnt[-uf.table[i]] += 1; auto dp = new int[](N+1); dp.fill(INF); dp[0] = -1; foreach (i; cnt.keys) { for (int j = 1; j <= cnt[i]; j *= 2) for (int k = N - 1; k >= 0; --k) if (k + i * j <= N) dp[k + i * j] = min(dp[k + i * j], dp[k] + j); int j = cnt[i] - (1 << bsr(cnt[i])); for (int k = N - 1; k >= 0; --k) if (k + i * j <= N) dp[k + i * j] = min(dp[k + i * j], dp[k] + j); } iota(1, N+1).map!(i => dp[i] == INF ? -1 : dp[i]).each!writeln; } class UnionFind { int N; int[] table; this(int n) { N = n; table = new int[](N); fill(table, -1); } int find(int x) { return table[x] < 0 ? x : (table[x] = find(table[x])); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (table[x] > table[y]) swap(x, y); table[x] += table[y]; table[y] = x; } }