結果

問題 No.317 辺の追加
ユーザー maine_honzukimaine_honzuki
提出日時 2021-05-31 09:36:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 290 ms / 2,000 ms
コード長 1,681 bytes
コンパイル時間 2,406 ms
コンパイル使用メモリ 210,340 KB
実行使用メモリ 8,192 KB
最終ジャッジ日時 2024-04-26 07:59:30
合計ジャッジ時間 12,943 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 162 ms
6,940 KB
testcase_03 AC 169 ms
6,940 KB
testcase_04 AC 186 ms
6,940 KB
testcase_05 AC 150 ms
6,944 KB
testcase_06 AC 194 ms
6,940 KB
testcase_07 AC 64 ms
6,944 KB
testcase_08 AC 238 ms
6,940 KB
testcase_09 AC 216 ms
6,940 KB
testcase_10 AC 244 ms
6,940 KB
testcase_11 AC 173 ms
8,192 KB
testcase_12 AC 254 ms
6,940 KB
testcase_13 AC 184 ms
7,680 KB
testcase_14 AC 224 ms
6,944 KB
testcase_15 AC 242 ms
6,940 KB
testcase_16 AC 205 ms
6,940 KB
testcase_17 AC 230 ms
6,940 KB
testcase_18 AC 240 ms
6,944 KB
testcase_19 AC 243 ms
6,940 KB
testcase_20 AC 215 ms
6,940 KB
testcase_21 AC 97 ms
6,940 KB
testcase_22 AC 52 ms
6,944 KB
testcase_23 AC 53 ms
6,944 KB
testcase_24 AC 171 ms
6,940 KB
testcase_25 AC 118 ms
6,940 KB
testcase_26 AC 126 ms
6,940 KB
testcase_27 AC 106 ms
6,940 KB
testcase_28 AC 54 ms
6,944 KB
testcase_29 AC 14 ms
6,940 KB
testcase_30 AC 271 ms
6,940 KB
testcase_31 AC 275 ms
6,940 KB
testcase_32 AC 285 ms
6,940 KB
testcase_33 AC 280 ms
6,940 KB
testcase_34 AC 281 ms
6,940 KB
testcase_35 AC 290 ms
6,940 KB
testcase_36 AC 282 ms
6,944 KB
testcase_37 AC 277 ms
6,944 KB
testcase_38 AC 264 ms
6,944 KB
testcase_39 AC 279 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//https://ncode.syosetu.com/n4830bu/317/
#include <bits/stdc++.h>
using namespace std;

struct UF {
    vector<int> data;
    UF(int n) : data(n, -1) {}
    int root(int k) {
        if (data[k] < 0)
            return k;
        return data[k] = root(data[k]);
    }
    bool unite(int x, int y) {
        x = root(x), y = root(y);
        if (x == y)
            return false;
        if (data[x] > data[y])
            swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return true;
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    int size(int k) { return -data[root(k)]; }
};

int main() {
    int N, M;
    cin >> N >> M;
    UF uf(N);
    while (M--) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        uf.unite(u, v);
    }
    map<int, int> mp;
    set<int> seen;
    for (int i = 0; i < N; i++) {
        int r = uf.root(i);
        if (!seen.count(r)) {
            seen.insert(r);
            mp[uf.size(r)]++;
        }
    }
    const int INF = 1e6;
    vector<int> dp(N + 1, INF);
    dp[0] = -1;
    for (auto [key, cnt] : mp) {
        auto update = [&](int n, int cost) {
            for (int i = N; i >= 0; i--) {
                int bef = i - n;
                if (bef < 0)
                    return;
                dp[i] = min(dp[i], dp[bef] + cost);
            }
        };
        for (int i = 0;; i++) {
            int n = 1 << i;
            if (n > cnt)
                break;
            cnt -= n;
            update(n * key, n);
        }
        update(cnt * key, cnt);
    }
    for (int i = 1; i <= N; i++) {
        cout << (dp[i] == INF ? -1 : dp[i]) << endl;
    }
}
0