結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 167 ms
5,128 KB
testcase_03 AC 170 ms
4,776 KB
testcase_04 AC 177 ms
5,340 KB
testcase_05 AC 150 ms
4,348 KB
testcase_06 AC 194 ms
4,776 KB
testcase_07 AC 63 ms
4,348 KB
testcase_08 AC 217 ms
5,572 KB
testcase_09 AC 215 ms
5,568 KB
testcase_10 AC 253 ms
5,568 KB
testcase_11 AC 147 ms
5,576 KB
testcase_12 AC 250 ms
5,568 KB
testcase_13 AC 158 ms
5,584 KB
testcase_14 AC 227 ms
5,568 KB
testcase_15 AC 244 ms
5,568 KB
testcase_16 AC 198 ms
5,568 KB
testcase_17 AC 232 ms
5,568 KB
testcase_18 AC 251 ms
5,568 KB
testcase_19 AC 253 ms
5,568 KB
testcase_20 AC 200 ms
5,568 KB
testcase_21 AC 101 ms
4,512 KB
testcase_22 AC 55 ms
4,348 KB
testcase_23 AC 56 ms
4,348 KB
testcase_24 AC 180 ms
5,328 KB
testcase_25 AC 125 ms
4,776 KB
testcase_26 AC 130 ms
4,776 KB
testcase_27 AC 104 ms
4,512 KB
testcase_28 AC 56 ms
4,348 KB
testcase_29 AC 15 ms
4,348 KB
testcase_30 AC 341 ms
5,568 KB
testcase_31 AC 371 ms
5,568 KB
testcase_32 AC 373 ms
5,568 KB
testcase_33 AC 375 ms
5,568 KB
testcase_34 AC 370 ms
5,568 KB
testcase_35 AC 375 ms
5,568 KB
testcase_36 AC 372 ms
5,568 KB
testcase_37 AC 377 ms
5,568 KB
testcase_38 AC 340 ms
5,568 KB
testcase_39 AC 372 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){
            deque<int> dq;
            for(int k=0; j+k*i<=n; ++k){
                if(!dq.empty())
                    nextDp[j+k*i] = min(nextDp[j+k*i], dq.front() + k);

                int x = dp[j+k*i] - k;
                while(!dq.empty() && x < dq.back())
                    dq.pop_back();
                dq.push_back(x);

                if(k >= a[i]){
                    int y = dp[j+(k-a[i])*i] - (k - a[i]);
                    if(dq.front() == y)
                        dq.pop_front();
                }
            }
        }
        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