結果

問題 No.317 辺の追加
ユーザー maru143maru143
提出日時 2021-05-02 15:07:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,439 bytes
コンパイル時間 2,177 ms
コンパイル使用メモリ 209,528 KB
実行使用メモリ 5,316 KB
最終ジャッジ日時 2023-09-28 05:33:58
合計ジャッジ時間 10,443 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 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 68 ms
4,376 KB
testcase_22 AC 36 ms
4,376 KB
testcase_23 WA -
testcase_24 AC 113 ms
4,380 KB
testcase_25 AC 83 ms
4,380 KB
testcase_26 AC 86 ms
4,380 KB
testcase_27 AC 68 ms
4,376 KB
testcase_28 AC 39 ms
4,384 KB
testcase_29 AC 11 ms
4,380 KB
testcase_30 WA -
testcase_31 AC 151 ms
4,376 KB
testcase_32 AC 154 ms
4,376 KB
testcase_33 AC 171 ms
4,376 KB
testcase_34 AC 158 ms
4,376 KB
testcase_35 AC 149 ms
4,376 KB
testcase_36 AC 150 ms
4,380 KB
testcase_37 AC 155 ms
4,376 KB
testcase_38 WA -
testcase_39 AC 149 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; j--) {
                dp[j] = min(dp[j], dp[j - x] + 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