結果
問題 | No.317 辺の追加 |
ユーザー | Pachicobue |
提出日時 | 2017-11-18 14:08:43 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,337 bytes |
コンパイル時間 | 2,100 ms |
コンパイル使用メモリ | 184,784 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-11-25 11:32:49 |
合計ジャッジ時間 | 11,875 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | RE | - |
testcase_03 | RE | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | WA | - |
testcase_12 | RE | - |
testcase_13 | WA | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | WA | - |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | RE | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
ソースコード
#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 < N; 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> value; vector<int> weight; for (int i = 0; i < size; i++) { for (int j = 1; j <= m[i]; j *= 2) { value.push_back(a[i] * j); weight.push_back(j); } } const int n = value.size(); vector<int> dp(N + 1, INF<int>); dp[0] = -1; for (int i = 0; i < n; i++) { auto tmp = dp; for (int j = 0; j <= N; j++) { if (tmp[j] != INF<int> and j + value[i] <= N) { dp[j + value[i]] = min(dp[j + value[i]], tmp[j] + weight[i]); } } } for (int i = 1; i <= N; i++) { if (dp[i] != INF<int>) { cout << dp[i] << endl; } else { cout << -1 << endl; } } return 0; }