結果

問題 No.317 辺の追加
ユーザー maru143
提出日時 2021-05-02 15:09:40
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 1,447 bytes
コンパイル時間 2,328 ms
コンパイル使用メモリ 204,868 KB
最終ジャッジ日時 2025-01-21 05:49:12
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#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 * m; j--) {
dp[j] = min(dp[j], dp[j - x * m] + 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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0