結果

問題 No.317 辺の追加
ユーザー ichyoichyo
提出日時 2015-12-10 01:08:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 2,209 bytes
コンパイル時間 1,727 ms
コンパイル使用メモリ 154,892 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-13 09:56:04
合計ジャッジ時間 9,073 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 118 ms
4,348 KB
testcase_03 AC 106 ms
4,348 KB
testcase_04 AC 132 ms
4,352 KB
testcase_05 AC 85 ms
4,348 KB
testcase_06 AC 115 ms
4,348 KB
testcase_07 AC 40 ms
4,348 KB
testcase_08 AC 157 ms
4,348 KB
testcase_09 AC 151 ms
4,348 KB
testcase_10 AC 161 ms
4,348 KB
testcase_11 AC 135 ms
4,348 KB
testcase_12 AC 159 ms
4,348 KB
testcase_13 AC 141 ms
4,352 KB
testcase_14 AC 157 ms
4,348 KB
testcase_15 AC 162 ms
4,348 KB
testcase_16 AC 150 ms
4,352 KB
testcase_17 AC 157 ms
4,348 KB
testcase_18 AC 162 ms
4,352 KB
testcase_19 AC 161 ms
4,348 KB
testcase_20 AC 151 ms
4,348 KB
testcase_21 AC 72 ms
4,352 KB
testcase_22 AC 41 ms
4,348 KB
testcase_23 AC 41 ms
4,348 KB
testcase_24 AC 128 ms
4,348 KB
testcase_25 AC 91 ms
4,348 KB
testcase_26 AC 94 ms
4,348 KB
testcase_27 AC 75 ms
4,348 KB
testcase_28 AC 41 ms
4,348 KB
testcase_29 AC 10 ms
4,348 KB
testcase_30 AC 167 ms
4,352 KB
testcase_31 AC 164 ms
4,348 KB
testcase_32 AC 167 ms
4,348 KB
testcase_33 AC 169 ms
4,352 KB
testcase_34 AC 167 ms
4,348 KB
testcase_35 AC 167 ms
4,352 KB
testcase_36 AC 164 ms
4,352 KB
testcase_37 AC 164 ms
4,352 KB
testcase_38 AC 166 ms
4,352 KB
testcase_39 AC 166 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Template {{{
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)
using namespace std;
typedef long long LL;

#ifdef LOCAL
#include "contest.h"
#else
#define dump(x) 
#endif

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
inline bool valid(int x, int w) { return 0 <= x && x < w; }

void iostream_init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.setf(ios::fixed);
    cout.precision(12);
}
//}}}

struct UnionFind {
    vector<int> data;
    UnionFind(int N) : data(N, -1) { }
    // xとyを併合する
    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
            if (data[x] > data[y]) swap(x, y);
            data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    // xとyが同じ集合にあるか判定する
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    // xを含む集合の要素数を求める
    int size(int x) {
        return -data[root(x)];
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
};

int main(){
    iostream_init();
    int N, M;
    while(cin >> N >> M) {
        UnionFind uf(N);
        REP(i, M) {
            int a, b;
            cin >> a >> b;
            a--; b--;
            uf.unite(a, b);
        }
        map<int, int> counter;
        REP(i, N) if(uf.root(i) == i) {
            int size = uf.size(i);
            counter[size] ++;
        }


        const int INF = 1e9;
        vector<int> min_cost(N + 1, INF);
        min_cost[0] = 0;
        for(const auto& p : counter) {
            int size = p.first;
            int count = p.second;
            for(int k = 0; count > 0; k++) {
                int use = min(count, 1 << k);
                int value = size * use;
                int cost  =    1 * use;
                for(int i = N - value; i >= 0; i--) {
                    min_cost[i + value] = min(min_cost[i + value], min_cost[i] + cost);
                }
                count -= use;
            }
        }

        REP(i, N) {
            int ans = min_cost[i+1];
            cout << (ans == INF ? -1 : ans-1) << endl;
        }
    }
    return 0;
}
0