結果

問題 No.317 辺の追加
ユーザー vjudge1
提出日時 2025-01-19 17:06:41
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 1,237 bytes
コンパイル時間 3,424 ms
コンパイル使用メモリ 283,312 KB
実行使用メモリ 28,476 KB
最終ジャッジ日時 2025-01-19 17:06:50
合計ジャッジ時間 8,607 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:28:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   28 |                 scanf("%lld %lld", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
long long n, m, e, h[1145141], ne[1145141], t[1145141], u, v, b[1145141], is[1145141], cnt, f[114514], cntt, cnttt;
struct AAA {
	int w, v;
} c[1145141];
map<long long, long long>ma;
int ct (int x, int y) {
	e++;
	ne[e] = h[x];
	h[x] = e;
	t[e] = y;
	return 0;
}
int dfs(int x, int y) {
	is[x] = 1;
	b[y]++;
	for (int i = h[x]; i; i = ne[i]) {
		if (!is[t[i]]) {
			dfs(t[i], y);
		}
	}
	return 0;
}
int main () {
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		scanf("%lld %lld", &u, &v);
		ct(u, v);
		ct(v, u);
	}
	for (int i = 1; i <= n; i++) {
		if(!is[i]) {
			dfs(i, ++cnt);
		}
	}
	for (int j = 1; j <= n; j++) {
		f[j] = 11451419;
	}
	for (int i = 1; i <= cnt; i++) {
		ma[b[i]]++;
	}
	for (int i = 1; i <= n; i++) {
		int blg = 1;
		while (ma[i] >= blg) {
			ma[i] -= blg;
			c[++cnttt].w = blg * i;
			c[cnttt].v = blg;
			blg *= 2;
		}
		if (ma[i]) {
			c[++cnttt].w = ma[i] * i;
			c[cnttt].v = ma[i];
		}
	}
	f[0] = 0;
	for (int i = 1; i <= cnttt; i++) {
		for (int j = n; j >= c[i].w; j--) {
			f[j] = min(f[j - c[i].w] + c[i].v, f[j]);
		}
//		f[c[i].w] = 0;
	}
	for (int i = 1; i <= n; i++) {
		if (f[i] >= 11451419) cout << "-1\n";
		else
			cout << f[i] - 1 << "\n";
	}
}
0