結果

問題 No.317 辺の追加
ユーザー pekempeypekempey
提出日時 2015-12-11 12:16:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 2,097 bytes
コンパイル時間 1,356 ms
コンパイル使用メモリ 159,100 KB
実行使用メモリ 5,032 KB
最終ジャッジ日時 2023-10-13 10:53:23
合計ジャッジ時間 6,360 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 35 ms
4,352 KB
testcase_03 AC 40 ms
4,348 KB
testcase_04 AC 38 ms
4,516 KB
testcase_05 AC 39 ms
4,352 KB
testcase_06 AC 48 ms
4,416 KB
testcase_07 AC 17 ms
4,348 KB
testcase_08 AC 76 ms
4,676 KB
testcase_09 AC 44 ms
4,552 KB
testcase_10 AC 56 ms
4,424 KB
testcase_11 AC 20 ms
5,032 KB
testcase_12 AC 56 ms
4,472 KB
testcase_13 AC 29 ms
4,956 KB
testcase_14 AC 47 ms
4,480 KB
testcase_15 AC 54 ms
4,476 KB
testcase_16 AC 40 ms
4,648 KB
testcase_17 AC 50 ms
4,608 KB
testcase_18 AC 55 ms
4,516 KB
testcase_19 AC 57 ms
4,440 KB
testcase_20 AC 45 ms
4,584 KB
testcase_21 AC 20 ms
4,348 KB
testcase_22 AC 13 ms
4,348 KB
testcase_23 AC 12 ms
4,348 KB
testcase_24 AC 35 ms
4,352 KB
testcase_25 AC 24 ms
4,352 KB
testcase_26 AC 26 ms
4,432 KB
testcase_27 AC 20 ms
4,352 KB
testcase_28 AC 13 ms
4,348 KB
testcase_29 AC 4 ms
4,352 KB
testcase_30 AC 117 ms
4,480 KB
testcase_31 AC 137 ms
4,532 KB
testcase_32 AC 137 ms
4,472 KB
testcase_33 AC 136 ms
4,520 KB
testcase_34 AC 136 ms
4,444 KB
testcase_35 AC 137 ms
4,448 KB
testcase_36 AC 137 ms
4,480 KB
testcase_37 AC 140 ms
4,352 KB
testcase_38 AC 118 ms
4,460 KB
testcase_39 AC 138 ms
4,444 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);
	}
};

struct SlideMin {
	int size;
	vector<int> a;
	deque<int> deq;
	SlideMin(int size) : size(size) {}
	void push(int v) {
		if (!deq.empty() && deq.front() == a.size() - size) deq.pop_front();
		while (!deq.empty() && a[deq.back()] > v) deq.pop_back();
		deq.push_back(a.size());
		a.push_back(v);
	}
	int query() {
		if (deq.empty()) return inf;
		return a[deq.front()];
	}
};

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;
		sum += val * num;

		for (int i = 0; i < val; i++) {
			SlideMin sm(num + 1);
			for (int j = 0; j * val + i <= sum; j++) {
				int k = j * val + i;
				sm.push(dp[k] - j);
				chmin(dp[k], sm.query() + j);
			}
		}
	}

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