結果

問題 No.317 辺の追加
ユーザー pekempeypekempey
提出日時 2015-12-11 13:04:28
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,643 bytes
コンパイル時間 2,179 ms
コンパイル使用メモリ 152,788 KB
実行使用メモリ 4,736 KB
最終ジャッジ日時 2023-10-13 10:54:03
合計ジャッジ時間 4,937 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 16 ms
4,348 KB
testcase_22 AC 10 ms
4,352 KB
testcase_23 WA -
testcase_24 AC 27 ms
4,348 KB
testcase_25 AC 19 ms
4,352 KB
testcase_26 AC 21 ms
4,356 KB
testcase_27 AC 17 ms
4,348 KB
testcase_28 WA -
testcase_29 AC 4 ms
4,348 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 66 ms
4,496 KB
testcase_33 AC 67 ms
4,488 KB
testcase_34 WA -
testcase_35 AC 67 ms
4,432 KB
testcase_36 AC 66 ms
4,492 KB
testcase_37 AC 68 ms
4,348 KB
testcase_38 WA -
testcase_39 AC 66 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;
	for (auto kv : mp) {
		int val = kv.first, num = kv.second;
		for (int k = 1; num > 0; k <<= 1) {
			int c = min(k, num);
			repr (j, val * c, n + 1) {
				chmin(dp[j], dp[j - val * c] + k);
			}
			num -= c;
		}
	}

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