結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 33 ms
4,348 KB
testcase_03 AC 36 ms
4,348 KB
testcase_04 AC 39 ms
4,508 KB
testcase_05 AC 36 ms
4,352 KB
testcase_06 AC 44 ms
4,348 KB
testcase_07 AC 15 ms
4,352 KB
testcase_08 AC 69 ms
4,920 KB
testcase_09 AC 42 ms
4,728 KB
testcase_10 AC 52 ms
4,684 KB
testcase_11 AC 20 ms
5,124 KB
testcase_12 AC 52 ms
4,732 KB
testcase_13 AC 26 ms
5,192 KB
testcase_14 AC 44 ms
4,752 KB
testcase_15 AC 49 ms
4,624 KB
testcase_16 AC 41 ms
4,928 KB
testcase_17 AC 46 ms
4,720 KB
testcase_18 AC 52 ms
4,624 KB
testcase_19 AC 52 ms
4,880 KB
testcase_20 AC 45 ms
4,984 KB
testcase_21 AC 19 ms
4,352 KB
testcase_22 AC 11 ms
4,352 KB
testcase_23 AC 11 ms
4,348 KB
testcase_24 AC 29 ms
4,372 KB
testcase_25 AC 25 ms
4,352 KB
testcase_26 AC 23 ms
4,352 KB
testcase_27 AC 20 ms
4,348 KB
testcase_28 AC 11 ms
4,348 KB
testcase_29 AC 4 ms
4,348 KB
testcase_30 AC 172 ms
4,716 KB
testcase_31 AC 196 ms
4,568 KB
testcase_32 AC 198 ms
4,640 KB
testcase_33 AC 197 ms
4,680 KB
testcase_34 AC 192 ms
4,672 KB
testcase_35 AC 200 ms
4,728 KB
testcase_36 AC 197 ms
4,848 KB
testcase_37 AC 200 ms
4,688 KB
testcase_38 AC 169 ms
4,568 KB
testcase_39 AC 197 ms
4,672 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.empty() && 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