結果

問題 No.317 辺の追加
コンテスト
ユーザー vjudge1
提出日時 2026-02-03 11:34:51
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,287 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,960 ms
コンパイル使用メモリ 335,420 KB
実行使用メモリ 7,972 KB
最終ジャッジ日時 2026-02-03 11:34:58
合計ジャッジ時間 6,396 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#define ll long long
#define inf 2147483647 //9223372036854775807
using namespace std;

inline int rd() {
	int x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); }
	while(ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar();
	return x * f;
}

int fa[100005], siz[100005], cnt[100005], v[100005], w[100005], f[100005];

int fd(int x) { 
	return fa[x] == x ? x : fa[x] = fd(fa[x]); 
}
signed main() {
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	int n = rd(), m = rd();
	for(int i = 1; i <= n; i++) fa[i] = i, siz[i] = 1;
	for(int i = 1; i <= m; i++) {
		int x = fd(rd()), y = fd(rd());
		if(x == y) continue;
		fa[y] = x, siz[x] += siz[y];
	}
	for(int i = 1; i <= n; i++) if(fd(i) == i) cnt[siz[i]]++;
	int tot = 0;
	for(int i = 1; i <= n; i++) if(cnt[i]) 
		for(int j = 1; cnt[i]; j <<= 1) 
			if(cnt[i] >= j) cnt[i] -= j, w[++tot] = i * j, v[tot] = j;
			else w[++tot] = i * cnt[i], v[tot] = cnt[i], cnt[i] = 0;
	for(int i = 1; i <= n; i++) f[i] = 1e9;
	for(int i = 1; i <= tot; i++) 
		for(int j = n; j >= w[i]; j--) 
			f[j] = min(f[j], f[j - w[i]] + v[i]);
	for(int i = 1; i <= n; i++) 
		if(f[i] < 1e9) cout << f[i] - 1 << '\n';
		else cout << -1 << '\n';
	return 0;
}
/*

*/
0