結果

問題 No.317 辺の追加
ユーザー nebukuro09nebukuro09
提出日時 2017-11-14 09:26:54
言語 D
(dmd 2.107.1)
結果
WA  
実行時間 -
コード長 1,534 bytes
コンパイル時間 3,566 ms
コンパイル使用メモリ 103,556 KB
実行使用メモリ 6,352 KB
最終ジャッジ日時 2023-09-03 16:54:08
合計ジャッジ時間 9,075 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 47 ms
4,380 KB
testcase_22 AC 26 ms
4,380 KB
testcase_23 WA -
testcase_24 AC 80 ms
4,380 KB
testcase_25 AC 58 ms
4,380 KB
testcase_26 AC 61 ms
4,376 KB
testcase_27 AC 50 ms
4,380 KB
testcase_28 WA -
testcase_29 AC 8 ms
4,376 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 224 ms
4,376 KB
testcase_33 AC 226 ms
4,388 KB
testcase_34 WA -
testcase_35 AC 227 ms
4,432 KB
testcase_36 AC 225 ms
4,380 KB
testcase_37 AC 227 ms
4,408 KB
testcase_38 WA -
testcase_39 AC 224 ms
4,380 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 <= 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;
    }
}
0