結果
問題 | No.317 辺の追加 |
ユーザー | Pachicobue |
提出日時 | 2017-11-18 14:13:49 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,346 bytes |
コンパイル時間 | 2,004 ms |
コンパイル使用メモリ | 186,676 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-11-25 11:33:38 |
合計ジャッジ時間 | 8,666 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | AC | 70 ms
6,820 KB |
testcase_22 | AC | 39 ms
6,816 KB |
testcase_23 | WA | - |
testcase_24 | AC | 121 ms
6,820 KB |
testcase_25 | AC | 86 ms
6,816 KB |
testcase_26 | AC | 91 ms
6,820 KB |
testcase_27 | AC | 71 ms
6,816 KB |
testcase_28 | WA | - |
testcase_29 | AC | 11 ms
6,820 KB |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | AC | 165 ms
6,816 KB |
testcase_33 | AC | 162 ms
6,820 KB |
testcase_34 | WA | - |
testcase_35 | AC | 166 ms
6,820 KB |
testcase_36 | AC | 162 ms
6,816 KB |
testcase_37 | AC | 163 ms
6,820 KB |
testcase_38 | WA | - |
testcase_39 | AC | 165 ms
6,820 KB |
ソースコード
#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> 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; }