結果

問題 No.317 辺の追加
ユーザー pekempeypekempey
提出日時 2015-12-11 13:10:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 1,678 bytes
コンパイル時間 1,466 ms
コンパイル使用メモリ 153,452 KB
実行使用メモリ 4,520 KB
最終ジャッジ日時 2023-10-13 10:54:46
合計ジャッジ時間 4,818 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 29 ms
4,352 KB
testcase_03 AC 35 ms
4,348 KB
testcase_04 AC 29 ms
4,372 KB
testcase_05 AC 36 ms
4,352 KB
testcase_06 AC 44 ms
4,352 KB
testcase_07 AC 15 ms
4,348 KB
testcase_08 AC 44 ms
4,348 KB
testcase_09 AC 36 ms
4,352 KB
testcase_10 AC 50 ms
4,352 KB
testcase_11 AC 19 ms
4,352 KB
testcase_12 AC 49 ms
4,352 KB
testcase_13 AC 26 ms
4,352 KB
testcase_14 AC 41 ms
4,352 KB
testcase_15 AC 47 ms
4,352 KB
testcase_16 AC 32 ms
4,352 KB
testcase_17 AC 43 ms
4,352 KB
testcase_18 AC 50 ms
4,352 KB
testcase_19 AC 51 ms
4,348 KB
testcase_20 AC 34 ms
4,348 KB
testcase_21 AC 16 ms
4,352 KB
testcase_22 AC 9 ms
4,400 KB
testcase_23 AC 10 ms
4,348 KB
testcase_24 AC 26 ms
4,352 KB
testcase_25 AC 19 ms
4,352 KB
testcase_26 AC 20 ms
4,348 KB
testcase_27 AC 17 ms
4,348 KB
testcase_28 AC 10 ms
4,348 KB
testcase_29 AC 4 ms
4,352 KB
testcase_30 AC 45 ms
4,464 KB
testcase_31 AC 43 ms
4,492 KB
testcase_32 AC 42 ms
4,432 KB
testcase_33 AC 42 ms
4,520 KB
testcase_34 AC 42 ms
4,516 KB
testcase_35 AC 42 ms
4,352 KB
testcase_36 AC 42 ms
4,480 KB
testcase_37 AC 43 ms
4,352 KB
testcase_38 AC 45 ms
4,352 KB
testcase_39 AC 42 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define GET_MACRO(a, b, c, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define rep2(i, a) rep3 (i, 0, a)
#define rep3(i, a, b) for (int i = (a); i < (b); i++)
#define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__)
#define repr2(i, a) repr3 (i, 0, a)
#define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--)
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
using namespace std;
typedef long long ll;

const int inf = 1e9;

struct UF {
	vector<int> parent, size;
	UF(int n) : parent(n), size(n, 1) {
		rep (i, n) parent[i] = i;
	}
	int root(int x) {
		if (x == parent[x]) return x;
		else return parent[x] = root(parent[x]);
	}
	void merge(int x, int y) {
		x = root(x); y = root(y);
		if (x == y) return;
		if (size[x] < size[y]) swap(x, y);
		size[x] += size[y];
		parent[y] = x;
	}
	bool same(int x, int y) {
		return root(x) == root(y);
	}
};

int dp[101010];

int main() {
	int n, m;
	cin >> n >> m;
	UF uf(n);
	rep (i, m) {
		int u, v;
		scanf("%d %d", &u, &v);
		u--; v--;
		uf.merge(u, v);
	}
	map<int, int> mp;
	rep (i, n) if (uf.root(i) == i) mp[uf.size[i]]++;
	rep (i, 101010) dp[i] = inf;

	dp[0] = 0;
	int sum = 0;
	for (auto kv : mp) {
		int val = kv.first, num = kv.second;
		for (int k = 1; num > 0; k <<= 1) {
			int c = min(k, num);
			sum += val * c;
			repr (j, val * c, sum + 1) {
				chmin(dp[j], dp[j - val * c] + c);
			}
			num -= c;
		}
	}

	rep (i, 1, n + 1) {
		if (dp[i] > n) dp[i] = 0;
		printf("%d\n", dp[i] - 1);
	}
	return 0;
}
0