結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 33 ms
4,408 KB
testcase_03 AC 36 ms
4,348 KB
testcase_04 AC 37 ms
4,684 KB
testcase_05 AC 36 ms
4,352 KB
testcase_06 AC 44 ms
4,352 KB
testcase_07 AC 15 ms
4,352 KB
testcase_08 AC 66 ms
4,984 KB
testcase_09 AC 41 ms
4,568 KB
testcase_10 AC 52 ms
4,636 KB
testcase_11 AC 20 ms
5,108 KB
testcase_12 AC 52 ms
4,736 KB
testcase_13 AC 25 ms
5,216 KB
testcase_14 AC 45 ms
4,628 KB
testcase_15 AC 49 ms
4,568 KB
testcase_16 AC 40 ms
5,000 KB
testcase_17 AC 46 ms
4,624 KB
testcase_18 AC 51 ms
4,624 KB
testcase_19 AC 52 ms
4,644 KB
testcase_20 AC 44 ms
5,004 KB
testcase_21 AC 19 ms
4,348 KB
testcase_22 AC 11 ms
4,348 KB
testcase_23 AC 11 ms
4,348 KB
testcase_24 AC 29 ms
4,360 KB
testcase_25 AC 25 ms
4,348 KB
testcase_26 AC 23 ms
4,348 KB
testcase_27 AC 19 ms
4,352 KB
testcase_28 AC 11 ms
4,348 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 162 ms
4,564 KB
testcase_31 AC 185 ms
4,740 KB
testcase_32 AC 187 ms
4,568 KB
testcase_33 AC 187 ms
4,728 KB
testcase_34 AC 183 ms
4,740 KB
testcase_35 AC 188 ms
4,720 KB
testcase_36 AC 185 ms
4,564 KB
testcase_37 AC 190 ms
4,672 KB
testcase_38 AC 161 ms
4,684 KB
testcase_39 AC 187 ms
4,628 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;
	for(int i = 1; i <= n; ++i){
		if(uf.find(i) == i){
			com.push_back(uf.cnt[i]);
		}
	}
	sort(com.begin(), com.end());
	com.push_back(-1);
	
	const int INF = 1010101010;
	vector<int> dp1(n + 1, INF), dp2;
	dp1[0] = 0;
	deque<pii> dq;
	
	for(int f = 0; com[f] != -1; ){
		int d = com[f];
		int t;
		for(t = f + 1; com[t] == d; ++t);
		int cnt = t - f;

		dp2 = dp1;
		for(int r = 0; r < d; ++r){
			dq.clear();
			for(int i = 0; i * d + r <= n; ++i){
				int idx = i * d + r;
				if(dq.front().second == i - cnt - 1){
					dq.pop_front();
				}
				int val = dp1[idx] - i;
				while(!dq.empty() && dq.back().first >= val){
					dq.pop_back();
				}
				dq.emplace_back(val, i);
				dp2[idx] = min(dp2[idx], dq.front().first + i);
			}
		}
		
		dp1.swap(dp2);
		f = t;
	}
	
	for(int i = 1; i <= n; ++i){
		printf("%d\n", dp1[i] == INF ? -1 : dp1[i] - 1);
	}
}
0