結果
問題 | No.317 辺の追加 |
ユーザー |
|
提出日時 | 2021-05-02 15:07:03 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,439 bytes |
コンパイル時間 | 2,096 ms |
コンパイル使用メモリ | 206,268 KB |
最終ジャッジ日時 | 2025-01-21 05:48:58 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 16 WA * 22 |
ソースコード
#include <bits/stdc++.h>using namespace std;using i64 = int64_t;struct UnionFind {vector<int> par;UnionFind(int n) : par(n, -1) {}int root(int x) {if (par[x] < 0) return x;return par[x] = root(par[x]);}bool unite(int x, int y) {int rx = root(x), ry = root(y);if (rx == ry) return false;if (size(rx) < size(ry)) swap(rx, ry);par[rx] += par[ry];par[ry] = rx;return true;}bool same(int x, int y) {return root(x) == root(y);}int size(int x) {return -par[root(x)];}};void solve() {int n, m;cin >> n >> m;UnionFind uf(n);for (int i = 0; i < m; i++) {int a, b;cin >> a >> b;a--;b--;uf.unite(a, b);}map<int, int> C;for (int i = 0; i < n; i++) {if (uf.root(i) == i) {C[uf.size(i)]++;}}vector<int> dp(n + 1, n + 1);dp[0] = 0;for (auto [x, num] : C) {for (int k = 1; num; k <<= 1) {int m = min(k, num);for (int j = n; j >= x; j--) {dp[j] = min(dp[j], dp[j - x] + m);}num -= m;}}for (int i = 1; i <= n; i++) {cout << (dp[i] == n + 1 ? -1 : dp[i] - 1) << endl;}}int main() {cin.tie(0);ios::sync_with_stdio(false);solve();return 0;}