結果

問題 No.317 辺の追加
ユーザー PachicobuePachicobue
提出日時 2017-08-14 19:09:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,790 bytes
コンパイル時間 2,409 ms
コンパイル使用メモリ 191,196 KB
実行使用メモリ 13,696 KB
最終ジャッジ日時 2024-04-21 11:26:45
合計ジャッジ時間 8,749 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,752 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1,851 ms
6,272 KB
testcase_03 AC 120 ms
5,376 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// {{{ Templates
#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 = (ll)1e9 + 7LL;

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> sizes;
    unordered_set<int> st;
    for (int i = 0; i < N; i++) {
        if (st.find(uf.find(i)) == st.end()) {
            st.insert(uf.find(i));
            sizes.push_back(uf.getSize(i));
        }
    }
    sort(sizes.begin(), sizes.end());
    auto sizes_cop = sizes;
    sizes_cop.erase(unique(sizes_cop.begin(), sizes_cop.end()), sizes_cop.end());
    const int size = sizes_cop.size();
    vector<map<int, int>> elem(size);
    for (int i = 0; i < size; i++) {
        const int n = upper_bound(sizes.begin(), sizes.end(), sizes_cop[i]) - lower_bound(sizes.begin(), sizes.end(), sizes_cop[i]);
        for (int j = 1; j <= n; j++) {
            elem[i][sizes_cop[i] * j] = j - 1;
        }
    }
    map<int, int> ans = elem[0];
    for (int i = 1; i < size; i++) {
        map<int, int> tmp = elem[i];
        for (const auto& p1 : ans) {
            for (const auto& p2 : elem[i]) {
                const int sum = p1.first + p2.first;
                const int edge = p1.second + p2.second + 1;
                if (tmp.find(sum) == tmp.end()) {
                    tmp[sum] = edge;
                } else {
                    tmp[sum] = min(tmp[sum], edge);
                }
            }
        }
        for (const auto& p : tmp) {
            const int num = p.first;
            const int edge = p.second;
            if (ans.find(num) == ans.end()) {
                ans[num] = edge;
            } else {
                ans[num] = min(ans[num], edge);
            }
        }
    }

    for (int i = 1; i <= N; i++) {
        if (ans.find(i) == ans.end()) {
            cout << -1 << endl;
        } else {
            cout << ans[i] << endl;
        }
    }


    return 0;
}
0