結果

問題 No.317 辺の追加
ユーザー catuppercatupper
提出日時 2015-12-09 23:38:50
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,351 bytes
コンパイル時間 551 ms
コンパイル使用メモリ 65,528 KB
実行使用メモリ 5,236 KB
最終ジャッジ日時 2023-10-13 08:53:17
合計ジャッジ時間 9,053 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 RE -
testcase_03 RE -
testcase_04 WA -
testcase_05 WA -
testcase_06 RE -
testcase_07 WA -
testcase_08 WA -
testcase_09 RE -
testcase_10 RE -
testcase_11 WA -
testcase_12 RE -
testcase_13 WA -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<deque>
using namespace std;

#define INF (1 << 29)

int dp[108000], dd[108000];
int mm, n, m, x, y;
int cnt[108000], res[108000];
int u[108000];

int r(int x){
	if(u[x] == x)return x;
	return u[x] = r(u[x]);
}

void unite(int x, int y){
//	if(r(x) == r(y))return;
	x = r(x);
	y = r(y);
	u[x] = y;
}

int main(){
	cin >> n >> m;
	for(int i = 1;i <= n;i++)u[i] = i;
	for(int i = 0;i < m;i++){
		cin >> x >> y;
		unite(x, y);
	}
	for(int i = 1;i <= n;i++){
		cnt[r(i)]++;
	}
	int pp = 0;
	for(int i = 1;i <= n;i++){
		if(cnt[i]){
			res[cnt[i]]++;
			pp++;
		}
	}
	fill(dp, dp + n + 3, INF);
	dp[0] = 0;
	for(int i = 1;i <= n;i++){
		if(res[i] == 0)continue;
		fill(dd, dd + n + 3, INF);
		deque<int> dq;
		int c = res[i], tmp = 0;
		for(int m = 0;m < i;m++){
			for(int j = 0;j*i+m <= n;j++){
				while(!dq.empty() && dp[dq.back() * i + m] - dq.back() >= dp[j*i+m]-j)dq.pop_back();
				dq.push_back(j);
				while(!dq.empty() && dq.front() < j - c)dq.pop_front();
				int ans = dq.front();
				
				if(dp[ans*i+m] == INF)dp[j*i+m] = INF;
				else dd[j * i + m] = dp[ans * i + m] + j - ans;
			}
		}
		for(int j = 0;j <= n;j++)dp[j] = min(INF, dd[j]);
	}                         
	return 0;
	for(int i = 1;i <= n;i++){
		if(dp[i] == INF)cout << -1 << endl;
		else cout << dp[i] - 1 << endl;
	}
	return 0;	
}
0