結果

問題 No.317 辺の追加
ユーザー parukiparuki
提出日時 2016-06-24 20:47:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,123 bytes
コンパイル時間 1,906 ms
コンパイル使用メモリ 174,860 KB
実行使用メモリ 5,632 KB
最終ジャッジ日時 2024-04-20 02:17:47
合計ジャッジ時間 6,158 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,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 16 ms
5,376 KB
testcase_22 AC 9 ms
5,376 KB
testcase_23 WA -
testcase_24 AC 25 ms
5,376 KB
testcase_25 AC 18 ms
5,376 KB
testcase_26 AC 19 ms
5,376 KB
testcase_27 AC 16 ms
5,376 KB
testcase_28 WA -
testcase_29 AC 3 ms
5,376 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 63 ms
5,376 KB
testcase_33 AC 66 ms
5,376 KB
testcase_34 WA -
testcase_35 AC 66 ms
5,632 KB
testcase_36 AC 64 ms
5,504 KB
testcase_37 AC 68 ms
5,376 KB
testcase_38 WA -
testcase_39 AC 65 ms
5,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

class UnionFind{
public:
    UnionFind(int _n):n(_n), cnt(_n), par(_n), rank(_n), size(_n, 1){
        for(int i=0;i<n;++i) par[i]=i;
    }
    int find(int k){
        return (k==par[k])?k:(par[k]=find(par[k]));
    }
    int operator[](int k){
        return find(k);
    }
    int getSize(int k){
        return size[find(k)];
    }
    void unite(int x, int y){
        x = find(x); y = find(y);
        if(x==y) return;
        --cnt;
        if(rank[x] < rank[y]){
            par[x] = y;
            size[y] += size[x];
        }else{
            par[y] = x;
            size[x] += size[y];
            if(rank[y] == rank[x]) ++rank[y];
        }
    }
    int count(){
        return cnt;
    }
private:
    int n, cnt;
    vector<int> par, rank, size;
};

const int NAX = 100001, INF = INT_MAX;
int N, M, dp[NAX];

int main(){
    while(cin >> N >> M){
        UnionFind uf(N + 1);
        rep(i, M){
            int u, v;
            scanf("%d%d", &u, &v);
            uf.unite(u, v);
        }

        vi cc(N + 1), w, cnt;
        FOR(i, 1, N + 1)cc[uf[i]]++;
        map<int, int> cnt_;
        FOR(i, 1, N + 1)if(cc[i])cnt_[cc[i]]++;
        each(p, cnt_)w.push_back(p.first), cnt.push_back(p.second);
        
        rep(j, N + 1)dp[j] = INF;
        dp[0] = -1;
        rep(i, sz(w))for(int m = 1; m <= cnt[i]; m <<= 1){
            for(int j = N; j >= 0; --j){
                int dw = m*w[i];
                if(j - dw >= 0 && dp[j - dw] != INF)
                    smin(dp[j], dp[j - dw] + m);
            }
        }
        FOR(i, 1, N + 1)printf("%d\n", dp[i] != INF ? dp[i] : -1);
    }
}
0