結果

問題 No.317 辺の追加
ユーザー nebukuro09nebukuro09
提出日時 2017-11-14 09:57:02
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 1,542 bytes
コンパイル時間 668 ms
コンパイル使用メモリ 105,992 KB
実行使用メモリ 5,936 KB
最終ジャッジ日時 2023-09-03 16:54:25
合計ジャッジ時間 7,282 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 90 ms
4,508 KB
testcase_03 AC 105 ms
4,380 KB
testcase_04 AC 94 ms
4,396 KB
testcase_05 AC 107 ms
4,384 KB
testcase_06 AC 130 ms
4,384 KB
testcase_07 AC 44 ms
4,380 KB
testcase_08 AC 128 ms
4,400 KB
testcase_09 AC 112 ms
4,380 KB
testcase_10 AC 150 ms
4,420 KB
testcase_11 AC 51 ms
4,392 KB
testcase_12 AC 149 ms
4,408 KB
testcase_13 AC 68 ms
4,416 KB
testcase_14 AC 123 ms
4,380 KB
testcase_15 AC 141 ms
4,460 KB
testcase_16 AC 104 ms
4,400 KB
testcase_17 AC 131 ms
4,404 KB
testcase_18 AC 150 ms
5,936 KB
testcase_19 AC 151 ms
4,380 KB
testcase_20 AC 108 ms
4,384 KB
testcase_21 AC 45 ms
4,380 KB
testcase_22 AC 25 ms
4,384 KB
testcase_23 AC 26 ms
5,676 KB
testcase_24 AC 79 ms
4,460 KB
testcase_25 AC 57 ms
4,384 KB
testcase_26 AC 60 ms
4,384 KB
testcase_27 AC 49 ms
5,888 KB
testcase_28 AC 26 ms
4,384 KB
testcase_29 AC 8 ms
4,384 KB
testcase_30 AC 170 ms
4,384 KB
testcase_31 AC 162 ms
4,396 KB
testcase_32 AC 160 ms
4,380 KB
testcase_33 AC 163 ms
4,392 KB
testcase_34 AC 160 ms
4,400 KB
testcase_35 AC 162 ms
4,424 KB
testcase_36 AC 163 ms
4,404 KB
testcase_37 AC 163 ms
4,384 KB
testcase_38 AC 170 ms
4,384 KB
testcase_39 AC 162 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 * 2 <= 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])) + 1;
        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;
    }
}
0