結果

問題 No.317 辺の追加
ユーザー pekempeypekempey
提出日時 2015-12-10 00:14:54
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,590 bytes
コンパイル時間 2,634 ms
コンパイル使用メモリ 151,308 KB
実行使用メモリ 9,232 KB
最終ジャッジ日時 2023-10-13 08:59:42
合計ジャッジ時間 7,915 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,700 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 74 ms
4,404 KB
testcase_03 AC 36 ms
4,352 KB
testcase_04 AC 520 ms
4,444 KB
testcase_05 AC 35 ms
4,348 KB
testcase_06 AC 43 ms
4,352 KB
testcase_07 AC 15 ms
4,352 KB
testcase_08 AC 1,354 ms
4,676 KB
testcase_09 AC 122 ms
4,604 KB
testcase_10 AC 54 ms
4,424 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

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;

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);
	}
};

const int inf = 1e9;
int dp[101010]; // min size

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);
	}
	vector<int> a;
	rep (i, n) if (uf.root(i) == i) a.push_back(uf.size[i]);
	sort(a.begin(), a.end());

	rep (i, 101010) dp[i] = inf;
	dp[0] = 0;
	int sum = 0;
	rep (i, a.size()) {
		sum += a[i];
		repr (j, a[i], sum + 1) {
			chmin(dp[j], dp[j - a[i]] + 1);
		}
	}
	rep (i, 1, n + 1) {
		if (dp[i] == inf) dp[i] = 0;
		printf("%d\n", dp[i] - 1);
	}
	return 0;
}
0