結果
| 問題 | No.317 辺の追加 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-11-12 23:14:26 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,519 bytes |
| 記録 | |
| コンパイル時間 | 2,145 ms |
| コンパイル使用メモリ | 156,416 KB |
| 実行使用メモリ | 13,880 KB |
| 最終ジャッジ日時 | 2024-06-12 22:30:33 |
| 合計ジャッジ時間 | 7,062 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 2 |
| other | TLE * 1 -- * 37 |
ソースコード
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;
cnt.writeln;
auto dp = new int[][](2, N+1);
dp[0].fill(INF);
dp[1].fill(INF);
dp[0][0] = -1;
int cur = 0;
int tar = 1;
foreach (i; cnt.keys) {
dp[tar] = dp[cur].dup;
for (int j = 0; j < N; ++j) {
for (int k = 1; k * i + j <= N && k <= cnt[i]; ++k) {
dp[tar][k*i+j] = min(dp[cur][k*i+j], dp[cur][j] + k);
}
}
cur ^= 1;
tar ^= 1;
}
iota(1, N+1).map!(i => dp[cur][i] == INF ? -1 : dp[cur][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;
}
}