結果

問題 No.317 辺の追加
ユーザー PachicobuePachicobue
提出日時 2017-11-18 14:45:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 191 ms / 2,000 ms
コード長 3,148 bytes
コンパイル時間 2,690 ms
コンパイル使用メモリ 181,036 KB
実行使用メモリ 5,940 KB
最終ジャッジ日時 2023-08-16 21:12:25
合計ジャッジ時間 10,829 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 126 ms
4,568 KB
testcase_03 AC 114 ms
4,380 KB
testcase_04 AC 144 ms
4,976 KB
testcase_05 AC 87 ms
4,380 KB
testcase_06 AC 116 ms
4,376 KB
testcase_07 AC 38 ms
4,376 KB
testcase_08 AC 178 ms
5,504 KB
testcase_09 AC 159 ms
5,208 KB
testcase_10 AC 163 ms
5,112 KB
testcase_11 AC 141 ms
5,940 KB
testcase_12 AC 169 ms
5,164 KB
testcase_13 AC 152 ms
5,712 KB
testcase_14 AC 162 ms
5,168 KB
testcase_15 AC 166 ms
5,176 KB
testcase_16 AC 162 ms
5,148 KB
testcase_17 AC 163 ms
5,176 KB
testcase_18 AC 161 ms
5,108 KB
testcase_19 AC 169 ms
5,100 KB
testcase_20 AC 164 ms
5,476 KB
testcase_21 AC 75 ms
4,380 KB
testcase_22 AC 42 ms
4,376 KB
testcase_23 AC 41 ms
4,376 KB
testcase_24 AC 128 ms
4,880 KB
testcase_25 AC 89 ms
4,388 KB
testcase_26 AC 98 ms
4,404 KB
testcase_27 AC 76 ms
4,376 KB
testcase_28 AC 40 ms
4,376 KB
testcase_29 AC 11 ms
4,376 KB
testcase_30 AC 186 ms
5,108 KB
testcase_31 AC 187 ms
5,120 KB
testcase_32 AC 185 ms
5,104 KB
testcase_33 AC 184 ms
5,320 KB
testcase_34 AC 181 ms
5,212 KB
testcase_35 AC 187 ms
5,108 KB
testcase_36 AC 186 ms
5,172 KB
testcase_37 AC 190 ms
5,196 KB
testcase_38 AC 191 ms
5,156 KB
testcase_39 AC 183 ms
5,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

class DisjointSets
{
public:
    DisjointSets(const int v)
    {
        m_parent.resize(v);
        m_rank.resize(v);
        m_size.resize(v);
        for (int i = 0; i < v; i++) {
            m_parent[i] = i;
            m_rank[i] = 0;
            m_size[i] = 1;
        }
    }

    bool same(const int a, const int b)
    {
        return find(a) == find(b);
    }


    int find(const int a)
    {
        if (m_parent[a] == a) {
            return a;
        } else {
            return m_parent[a] = find(m_parent[a]);
        }
    }

    void unite(const int a_, const int b_)
    {
        const int a = find(a_);
        const int b = find(b_);
        if (a == b) {
            return;
        }
        if (m_rank[a] > m_rank[b]) {
            m_parent[b] = a;
            m_size[a] += m_size[b];
        } else {
            m_parent[a] = b;
            m_size[b] += m_size[a];
        }
        if (m_rank[a] == m_rank[b]) {
            m_rank[b]++;
        }
    }

    int getSize(const int a)
    {
        return m_size[m_parent[a]];
    }

private:
    vector<int> m_parent;
    vector<int> m_rank;
    vector<int> m_size;
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    DisjointSets uf(N);
    for (int i = 0; i < M; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        uf.unite(u, v);
    }
    vector<int> node(N, 0);
    for (int i = 0; i < N; i++) {
        node[uf.find(i)]++;
    }
    vector<int> num;
    for (int i = 0; i < N; i++) {
        if (node[i] > 0) {
            num.push_back(node[i]);
        }
    }
    sort(num.begin(), num.end());
    map<int, int> mp;
    for (int i = 0; i < num.size(); i++) {
        mp[num[i]]++;
    }
    auto a = num;
    a.erase(unique(a.begin(), a.end()), a.end());
    const int size = a.size();
    vector<int> m(size, 0);
    for (int i = 0; i < size; i++) {
        m[i] = mp[a[i]];
    }

    vector<int> dp(N + 1, INF<int>);
    dp[0] = -1;
    for (int i = 0; i < size; i++) {
        int num = m[i];
        for (int k = 1; num > 0; k <<= 1) {
            const int mul = min(k, num);
            for (int j = N; j >= a[i] * mul; j--) {
                dp[j] = min(dp[j], dp[j - a[i] * mul] + mul);
            }
            num -= mul;
        }
    }

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

    return 0;
}
0