結果

問題 No.317 辺の追加
ユーザー climpetclimpet
提出日時 2015-12-10 02:13:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 1,501 bytes
コンパイル時間 533 ms
コンパイル使用メモリ 70,272 KB
実行使用メモリ 5,936 KB
最終ジャッジ日時 2023-10-13 10:12:24
合計ジャッジ時間 5,552 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 32 ms
5,276 KB
testcase_03 AC 35 ms
4,984 KB
testcase_04 AC 38 ms
5,536 KB
testcase_05 AC 37 ms
4,352 KB
testcase_06 AC 44 ms
5,008 KB
testcase_07 AC 15 ms
4,348 KB
testcase_08 AC 66 ms
5,804 KB
testcase_09 AC 41 ms
5,776 KB
testcase_10 AC 51 ms
5,752 KB
testcase_11 AC 18 ms
5,680 KB
testcase_12 AC 51 ms
5,784 KB
testcase_13 AC 24 ms
5,744 KB
testcase_14 AC 44 ms
5,680 KB
testcase_15 AC 48 ms
5,792 KB
testcase_16 AC 39 ms
5,688 KB
testcase_17 AC 44 ms
5,936 KB
testcase_18 AC 51 ms
5,736 KB
testcase_19 AC 52 ms
5,800 KB
testcase_20 AC 44 ms
5,680 KB
testcase_21 AC 18 ms
4,368 KB
testcase_22 AC 10 ms
4,348 KB
testcase_23 AC 11 ms
4,348 KB
testcase_24 AC 29 ms
5,428 KB
testcase_25 AC 22 ms
4,824 KB
testcase_26 AC 23 ms
5,008 KB
testcase_27 AC 19 ms
4,756 KB
testcase_28 AC 11 ms
4,348 KB
testcase_29 AC 4 ms
4,356 KB
testcase_30 AC 150 ms
5,888 KB
testcase_31 AC 174 ms
5,696 KB
testcase_32 AC 172 ms
5,692 KB
testcase_33 AC 173 ms
5,740 KB
testcase_34 AC 172 ms
5,676 KB
testcase_35 AC 176 ms
5,776 KB
testcase_36 AC 173 ms
5,684 KB
testcase_37 AC 176 ms
5,800 KB
testcase_38 AC 148 ms
5,684 KB
testcase_39 AC 169 ms
5,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <vector>
// #include <deque>
#include <numeric>
#include <algorithm>
#include <utility>
using namespace std;

typedef pair<int,int> pii;

struct UF{
	vector<int> par, cnt;
	explicit UF(int n) : par(n), cnt(n, 1) {
		iota(par.begin(), par.end(), 0);
	}
	int find(int x){
		return x != par[x] ? par[x] = find(par[x]) : x;
	}
	bool unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y){ return false; }
		if(cnt[x] > cnt[y]){ swap(x, y); }
		par[x] = y;
		cnt[y] += cnt[x];
		return true;
	}
};

int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	UF uf(n + 1);
	for(int i = 0; i < m; ++i){
		int u, v;
		scanf("%d%d", &u, &v);
		uf.unite(u, v);
	}
	vector<int> com(n + 1);
	for(int i = 1; i <= n; ++i){
		if(uf.find(i) == i){
			++com[uf.cnt[i]];
		}
	}
	
	const int INF = 1010101010;
	vector<int> dp1(n + 1, INF), dp2;
	dp1[0] = 0;
	vector<pii> buf(n + 9);
	
	for(int d = 1; d <= n; ++d){
		int cnt = com[d];
		if(!cnt){ continue; }

		dp2 = dp1;
		for(int r = 0; r < d; ++r){
			int head = 0, tail = 0;
			for(int i = 0; i * d + r <= n; ++i){
				int idx = i * d + r;
				if(head != tail && buf[head].second == i - cnt - 1){
					++head;
				}
				int val = dp1[idx] - i;
				while(head != tail && buf[tail - 1].first >= val){
					--tail;
				}
				buf[tail++] = pii(val, i);
				dp2[idx] = min(dp2[idx], buf[head].first + i);
			}
		}
		
		dp1.swap(dp2);
	}
	
	for(int i = 1; i <= n; ++i){
		printf("%d\n", dp1[i] == INF ? -1 : dp1[i] - 1);
	}
}
0