結果

問題 No.317 辺の追加
ユーザー maine_honzukimaine_honzuki
提出日時 2021-05-31 09:36:43
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 237 ms / 2,000 ms
コード長 1,681 bytes
コンパイル時間 2,425 ms
コンパイル使用メモリ 212,596 KB
実行使用メモリ 8,000 KB
最終ジャッジ日時 2023-08-08 15:22:32
合計ジャッジ時間 13,432 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 157 ms
4,376 KB
testcase_03 AC 157 ms
4,380 KB
testcase_04 AC 170 ms
5,424 KB
testcase_05 AC 138 ms
4,380 KB
testcase_06 AC 176 ms
4,376 KB
testcase_07 AC 57 ms
4,380 KB
testcase_08 AC 205 ms
6,008 KB
testcase_09 AC 203 ms
4,664 KB
testcase_10 AC 231 ms
4,376 KB
testcase_11 AC 165 ms
8,000 KB
testcase_12 AC 228 ms
4,380 KB
testcase_13 AC 177 ms
7,680 KB
testcase_14 AC 207 ms
4,380 KB
testcase_15 AC 221 ms
4,376 KB
testcase_16 AC 194 ms
5,244 KB
testcase_17 AC 212 ms
4,380 KB
testcase_18 AC 226 ms
4,380 KB
testcase_19 AC 230 ms
4,380 KB
testcase_20 AC 192 ms
5,500 KB
testcase_21 AC 91 ms
4,376 KB
testcase_22 AC 51 ms
4,380 KB
testcase_23 AC 52 ms
4,376 KB
testcase_24 AC 160 ms
4,376 KB
testcase_25 AC 116 ms
4,380 KB
testcase_26 AC 120 ms
4,376 KB
testcase_27 AC 95 ms
4,380 KB
testcase_28 AC 51 ms
4,376 KB
testcase_29 AC 13 ms
4,376 KB
testcase_30 AC 227 ms
4,380 KB
testcase_31 AC 236 ms
4,376 KB
testcase_32 AC 230 ms
4,380 KB
testcase_33 AC 232 ms
4,376 KB
testcase_34 AC 230 ms
4,376 KB
testcase_35 AC 237 ms
4,376 KB
testcase_36 AC 230 ms
4,376 KB
testcase_37 AC 229 ms
4,376 KB
testcase_38 AC 224 ms
4,380 KB
testcase_39 AC 227 ms
4,376 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