結果

問題 No.317 辺の追加
ユーザー mayoko_mayoko_
提出日時 2015-12-11 15:31:11
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,207 bytes
コンパイル時間 740 ms
コンパイル使用メモリ 82,568 KB
実行使用メモリ 4,836 KB
最終ジャッジ日時 2023-10-13 10:55:08
合計ジャッジ時間 7,771 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 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 76 ms
4,352 KB
testcase_22 AC 41 ms
4,352 KB
testcase_23 WA -
testcase_24 AC 127 ms
4,352 KB
testcase_25 AC 92 ms
4,348 KB
testcase_26 AC 97 ms
4,348 KB
testcase_27 AC 78 ms
4,348 KB
testcase_28 WA -
testcase_29 AC 11 ms
4,352 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 173 ms
4,592 KB
testcase_33 AC 169 ms
4,640 KB
testcase_34 WA -
testcase_35 AC 170 ms
4,648 KB
testcase_36 AC 168 ms
4,580 KB
testcase_37 AC 168 ms
4,640 KB
testcase_38 WA -
testcase_39 AC 168 ms
4,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
#include<complex>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

struct UnionFind {
    vector<int> par;
    int n, cnt;
    UnionFind(const int& x = 0) {init(x);}
    void init(const int& x) {par.assign(cnt=n=x, -1);}
    inline int find(const int& x) {return par[x] < 0 ? x : par[x] = find(par[x]);}
    inline bool same(const int& x, const int& y) {return find(x) == find(y);}
    inline bool unite(int x, int y) {
        if ((x = find(x)) == (y = find(y))) return false;
        --cnt;
        if (par[x] > par[y]) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    inline int count() const {return cnt;}
    inline int count(int x) {return -par[find(x)];}
};

const int INF = 1e8;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    UnionFind uf(N);
    for (int i = 0; i < M; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        uf.unite(u, v);
    }
    vector<int> memo(N);
    for (int i = 0; i < N; i++) memo[uf.find(i)]++;
    vector<int> cnt(N+1);
    for (int i = 0; i < N; i++) if (memo[i]) cnt[memo[i]]++;
//    for (int i = 1; i <= N; i++) {
//        cout << i << "  " << cnt[i] << endl;
//    }
    vector<int> ans(N+1, INF);
    ans[0] = 0;
    for (int i = 1; i <= N; i++) {
        for (int k = 20; k >= 0; k--) {
            if ((cnt[i]>>k)&1) {
                int num = 1<<k;
                for (int j = N-i*num; j >= 0; j--) {
                    int nj = j+i*num;
                    ans[nj] = min(ans[nj], ans[j]+num);
                }
            }
        }
    }
    for (int i = 1; i <= N; i++) cout << ((ans[i]==INF) ? -1 : ans[i]-1) << endl;
    return 0;
}
0