結果

問題 No.317 辺の追加
ユーザー mamekinmamekin
提出日時 2015-12-20 11:32:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,430 ms / 2,000 ms
コード長 2,634 bytes
コンパイル時間 1,149 ms
コンパイル使用メモリ 101,120 KB
実行使用メモリ 9,528 KB
最終ジャッジ日時 2023-10-17 14:11:29
合計ジャッジ時間 26,324 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 210 ms
5,464 KB
testcase_03 AC 182 ms
4,776 KB
testcase_04 AC 323 ms
6,360 KB
testcase_05 AC 154 ms
4,348 KB
testcase_06 AC 204 ms
4,776 KB
testcase_07 AC 64 ms
4,348 KB
testcase_08 AC 604 ms
7,152 KB
testcase_09 AC 277 ms
6,096 KB
testcase_10 AC 277 ms
5,660 KB
testcase_11 AC 222 ms
9,528 KB
testcase_12 AC 273 ms
5,656 KB
testcase_13 AC 276 ms
8,472 KB
testcase_14 AC 267 ms
5,832 KB
testcase_15 AC 278 ms
5,700 KB
testcase_16 AC 328 ms
6,624 KB
testcase_17 AC 266 ms
5,832 KB
testcase_18 AC 278 ms
5,660 KB
testcase_19 AC 271 ms
5,648 KB
testcase_20 AC 388 ms
6,888 KB
testcase_21 AC 116 ms
4,512 KB
testcase_22 AC 64 ms
4,348 KB
testcase_23 AC 69 ms
4,348 KB
testcase_24 AC 190 ms
5,324 KB
testcase_25 AC 143 ms
4,776 KB
testcase_26 AC 151 ms
4,776 KB
testcase_27 AC 125 ms
4,512 KB
testcase_28 AC 71 ms
4,348 KB
testcase_29 AC 15 ms
4,348 KB
testcase_30 AC 1,241 ms
5,568 KB
testcase_31 AC 1,372 ms
5,568 KB
testcase_32 AC 1,372 ms
5,568 KB
testcase_33 AC 1,381 ms
5,568 KB
testcase_34 AC 1,356 ms
5,568 KB
testcase_35 AC 1,393 ms
5,568 KB
testcase_36 AC 1,430 ms
5,568 KB
testcase_37 AC 1,401 ms
5,568 KB
testcase_38 AC 1,217 ms
5,568 KB
testcase_39 AC 1,375 ms
5,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

class UnionFindTree
{
private:
    int n;
    vector<int> parent; // 親ノード
    vector<int> rank;   // 木の高さの上限
    vector<int> num;    // グループの要素数
    int find(int i){
        if(parent[i] == i)
            return i;
        return parent[i] = find(parent[i]);
    }
public:
    UnionFindTree(int n0){ // コンストラクタ
        n = n0;
        parent.resize(n);
        for(int i=0; i<n; ++i)
            parent[i] = i;
        rank.assign(n, 0);
        num.assign(n, 1);
    }
    void unite(int a, int b){ // aとbのグループを併合
        if((a = find(a)) != (b = find(b))){
            if(rank[a] < rank[b]){
                parent[a] = b;
                num[b] += num[a];
            }else{
                parent[b] = a;
                if(rank[a] == rank[b])
                    ++ rank[a];
                num[a] += num[b];
            }
            -- n;
        }
    }
    bool same(int a, int b){ // aとbのグループが同じかを調べる
        return find(a) == find(b);
    }
    int getNum(){ // グループの数を返す
        return n;
    }
    int getNum(int a){ // aのグループの要素数を返す
        return num[find(a)];
    }
};

const int INF = INT_MAX / 2;

int main()
{
    int n, m;
    cin >> n >> m;

    UnionFindTree uft(n);
    for(int i=0; i<m; ++i){
        int u, v;
        cin >> u >> v;
        uft.unite(u-1, v-1);
    }

    vector<int> a(n+1);
    for(int i=0; i<n; ++i)
        ++ a[uft.getNum(i)];

    vector<int> dp(n+1, INF);
    dp[0] = -1;
    for(int i=1; i<=n; ++i){
        if(a[i] == 0)
            continue;
        a[i] /= i;

        vector<int> nextDp = dp;
        for(int j=0; j<i; ++j){
            multiset<int> ms;
            for(int k=0; j+k*i<=n; ++k){
                if(!ms.empty())
                    nextDp[j+k*i] = min(nextDp[j+k*i], *ms.begin() + k);

                ms.insert(dp[j+k*i] - k);
                if(k >= a[i])
                    ms.erase(ms.find(dp[j+(k-a[i])*i] - (k - a[i])));
            }
        }
        dp.swap(nextDp);
    }

    for(int i=1; i<=n; ++i){
        if(dp[i] < INF)
            cout << dp[i] << endl;
        else
            cout << -1 << endl;
    }

    return 0;
}
0