結果

問題 No.317 辺の追加
ユーザー nebukuro09nebukuro09
提出日時 2017-11-12 23:14:26
言語 D
(dmd 2.107.1)
結果
WA  
実行時間 -
コード長 1,519 bytes
コンパイル時間 2,247 ms
コンパイル使用メモリ 153,748 KB
実行使用メモリ 11,032 KB
最終ジャッジ日時 2023-09-03 16:52:45
合計ジャッジ時間 8,107 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
    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;
    }
}
0