結果

問題 No.317 辺の追加
ユーザー vjudge1
提出日時 2025-01-18 18:28:27
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 235 ms / 2,000 ms
コード長 1,090 bytes
コンパイル時間 679 ms
コンパイル使用メモリ 59,260 KB
実行使用メモリ 13,824 KB
最終ジャッジ日時 2025-01-18 18:28:38
合計ジャッジ時間 9,294 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;
typedef long long ll;
ll n, m, e[400002], h[100002], ne[400002], idx, dp[100002], cnt[100002], c[100002], az[100002], l, ans;
bool f[100002];
void add(ll x, ll y) {
	e[++ idx] = y, ne[idx] = h[x], h[x] = idx;
}
void dfs(ll x) {
	ans ++;
	f[x] = 1;
	for (ll i = h[x]; i; i = ne[i]) {
		ll y = e[i];
		if (! f[y]) dfs(y);
	}
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for (ll x, y; m --; ) cin >> x >> y, add(x, y), add(y, x);
	for (ll i = 1; i <= n; i ++ ) if (! f[i]) ans = 0, dfs(i), cnt[ans] ++;
	for (ll i = 1; i <= n; i ++ ) {
		if (! cnt[i]) continue;
		ll p = 1;
		//cout << i << "," << cnt[i] << ":";
		while (p <= cnt[i]) az[++ l] = p * i, c[l] = p, cnt[i] -= p, p <<= 1;
		//puts("");
		if (cnt[i]) az[++ l] = cnt[i] * i, c[l] = cnt[i];
	}
	for (ll i = 1; i <= n; i ++ ) dp[i] = 0x3f3f3f3f3f3f3f3f;
	dp[0] = 0;
	for (ll i = 1; i <= l; i ++ ) for (ll j = n; j >= az[i]; j -- ) dp[j] = min(dp[j], dp[j - az[i]] + c[i]);
	for (ll i = 1; i <= n; i ++ ) cout << (dp[i] > n ? -1ll : dp[i] - 1) << endl;
}
0