結果

問題 No.317 辺の追加
ユーザー maru143maru143
提出日時 2021-05-02 15:09:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 175 ms / 2,000 ms
コード長 1,447 bytes
コンパイル時間 5,663 ms
コンパイル使用メモリ 207,724 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-28 05:35:57
合計ジャッジ時間 10,670 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 124 ms
4,376 KB
testcase_03 AC 110 ms
4,380 KB
testcase_04 AC 136 ms
4,380 KB
testcase_05 AC 85 ms
4,376 KB
testcase_06 AC 117 ms
4,376 KB
testcase_07 AC 40 ms
4,376 KB
testcase_08 AC 160 ms
4,376 KB
testcase_09 AC 156 ms
4,380 KB
testcase_10 AC 165 ms
4,380 KB
testcase_11 AC 136 ms
4,380 KB
testcase_12 AC 164 ms
4,376 KB
testcase_13 AC 142 ms
4,376 KB
testcase_14 AC 159 ms
4,380 KB
testcase_15 AC 163 ms
4,380 KB
testcase_16 AC 154 ms
4,384 KB
testcase_17 AC 160 ms
4,376 KB
testcase_18 AC 164 ms
4,380 KB
testcase_19 AC 165 ms
4,376 KB
testcase_20 AC 153 ms
4,376 KB
testcase_21 AC 75 ms
4,376 KB
testcase_22 AC 40 ms
4,380 KB
testcase_23 AC 40 ms
4,500 KB
testcase_24 AC 130 ms
4,380 KB
testcase_25 AC 94 ms
4,380 KB
testcase_26 AC 99 ms
4,376 KB
testcase_27 AC 80 ms
4,380 KB
testcase_28 AC 42 ms
4,380 KB
testcase_29 AC 11 ms
4,380 KB
testcase_30 AC 175 ms
4,384 KB
testcase_31 AC 171 ms
4,376 KB
testcase_32 AC 170 ms
4,380 KB
testcase_33 AC 172 ms
4,380 KB
testcase_34 AC 168 ms
4,380 KB
testcase_35 AC 168 ms
4,380 KB
testcase_36 AC 168 ms
4,380 KB
testcase_37 AC 171 ms
4,380 KB
testcase_38 AC 170 ms
4,376 KB
testcase_39 AC 170 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;

struct UnionFind {
    vector<int> par;

    UnionFind(int n) : par(n, -1) {}

    int root(int x) {
        if (par[x] < 0) return x;
        return par[x] = root(par[x]);
    }

    bool unite(int x, int y) {
        int rx = root(x), ry = root(y);
        if (rx == ry) return false;
        if (size(rx) < size(ry)) swap(rx, ry);
        par[rx] += par[ry];
        par[ry] = rx;
        return true;
    }

    bool same(int x, int y) {
        return root(x) == root(y);
    }

    int size(int x) {
        return -par[root(x)];
    }
};

void solve() {
    int n, m;
    cin >> n >> m;
    UnionFind uf(n);
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        uf.unite(a, b);
    }
    map<int, int> C;
    for (int i = 0; i < n; i++) {
        if (uf.root(i) == i) {
            C[uf.size(i)]++;
        }
    }
    vector<int> dp(n + 1, n + 1);
    dp[0] = 0;

    for (auto [x, num] : C) {
        for (int k = 1; num; k <<= 1) {
            int m = min(k, num);
            for (int j = n; j >= x * m; j--) {
                dp[j] = min(dp[j], dp[j - x * m] + m);
            }
            num -= m;
        }
    }

    for (int i = 1; i <= n; i++) {
        cout << (dp[i] == n + 1 ? -1 : dp[i] - 1) << endl;
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
0