結果
問題 | No.317 辺の追加 |
ユーザー |
![]() |
提出日時 | 2025-01-19 10:28:48 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 53 ms / 2,000 ms |
コード長 | 1,407 bytes |
コンパイル時間 | 1,792 ms |
コンパイル使用メモリ | 193,588 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2025-01-19 10:28:55 |
合計ジャッジ時間 | 6,310 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 38 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:11:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 11 | scanf("%d%d", &n, &m); | ~~~~~^~~~~~~~~~~~~~~~ main.cpp:14:24: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 14 | int u, v; scanf("%d%d", &u, &v); | ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 15;int n, p[N], sz[N], m;int find(int x) { return (p[x] == x) ? x : p[x] = find(p[x]); }int cnt[N]; //cnt[i] ----> w:i, v:1bool st[N];int dp[N];int main() {scanf("%d%d", &n, &m);for (int i = 1; i <= n; i++) p[i] = i, sz[i] = 1;for (int i = 1; i <= m; i++) {int u, v; scanf("%d%d", &u, &v);u = find(u), v = find(v);if (u == v) continue;if (sz[u] > sz[v]) swap(u, v);p[u] = v, sz[v] += sz[u];}for (int i = 1; i <= n; i++) {int x = find(i);if (st[x]) continue;st[x] = 1;cnt[sz[x]]++;}// for (int i = 1; i <= n; i++) cout << cnt[i] << ' '; puts("");memset(dp, 0x3f, sizeof dp);dp[0] = 0;for (int i = 1; i <= n; i++) {int w = i, s = cnt[i];if (!s) continue;for (int k = 1; k <= s; k <<= 1) {for (int j = n; j >= k * w; j--) {dp[j] = min(dp[j], dp[j - k * w] + k);// if (j == n) cout << '\t' << j - k * w << ' ' << dp[j - k * w] + k << endl;}s -= k;}if (s)for (int j = n; j >= s * w; j--) dp[j] = min(dp[j], dp[j - s * w] + s);}for (int i = 1; i <= n; i++) {if (dp[i] == 0x3f3f3f3f) puts("-1");else printf("%d\n", dp[i] - 1);}return 0;}