結果

問題 No.317 辺の追加
ユーザー piyoko_212piyoko_212
提出日時 2015-12-21 20:49:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 249 ms / 2,000 ms
コード長 1,603 bytes
コンパイル時間 579 ms
コンパイル使用メモリ 47,956 KB
実行使用メモリ 208,388 KB
最終ジャッジ日時 2023-10-18 22:06:24
合計ジャッジ時間 9,783 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
101,960 KB
testcase_01 AC 21 ms
101,964 KB
testcase_02 AC 88 ms
136,440 KB
testcase_03 AC 89 ms
134,552 KB
testcase_04 AC 106 ms
135,500 KB
testcase_05 AC 81 ms
133,852 KB
testcase_06 AC 97 ms
135,452 KB
testcase_07 AC 50 ms
134,756 KB
testcase_08 AC 186 ms
136,596 KB
testcase_09 AC 103 ms
136,032 KB
testcase_10 AC 116 ms
135,988 KB
testcase_11 AC 91 ms
135,984 KB
testcase_12 AC 118 ms
135,988 KB
testcase_13 AC 107 ms
137,572 KB
testcase_14 AC 107 ms
139,508 KB
testcase_15 AC 114 ms
139,492 KB
testcase_16 AC 108 ms
139,608 KB
testcase_17 AC 109 ms
139,500 KB
testcase_18 AC 116 ms
139,488 KB
testcase_19 AC 117 ms
139,488 KB
testcase_20 AC 122 ms
139,644 KB
testcase_21 AC 61 ms
141,108 KB
testcase_22 AC 47 ms
140,836 KB
testcase_23 AC 47 ms
138,788 KB
testcase_24 AC 85 ms
139,392 KB
testcase_25 AC 69 ms
141,236 KB
testcase_26 AC 71 ms
139,220 KB
testcase_27 AC 62 ms
139,080 KB
testcase_28 AC 47 ms
140,836 KB
testcase_29 AC 33 ms
138,540 KB
testcase_30 AC 249 ms
202,068 KB
testcase_31 AC 224 ms
206,412 KB
testcase_32 AC 224 ms
207,204 KB
testcase_33 AC 224 ms
206,020 KB
testcase_34 AC 213 ms
205,624 KB
testcase_35 AC 219 ms
206,412 KB
testcase_36 AC 216 ms
206,020 KB
testcase_37 AC 223 ms
206,412 KB
testcase_38 AC 228 ms
204,832 KB
testcase_39 AC 219 ms
208,388 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:20:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   20 |         int a,b;scanf("%d%d",&a,&b);
      |                 ~~~~~^~~~~~~~~~~~~~
main.cpp:23:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   23 |                 int s,t;scanf("%d%d",&s,&t);s--;t--;r(s,t);
      |                         ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

#include<stdio.h>
#include<algorithm>
#include<deque>
using namespace std;
int p[110000];
int q(int a){
	if(p[a]<0)return a;
	return p[a]=q(p[a]);
}
void r(int a,int b){
	a=q(a);b=q(b);if(a==b)return;p[a]+=p[b];p[b]=a;
}
int SQ=300;
int cnt[1000];
int u[210000];
int v[21000];
int dp[310][111000];
int dp2[410][111000];
int main(){
	int a,b;scanf("%d%d",&a,&b);
	for(int i=0;i<a;i++)p[i]=-1;
	for(int i=0;i<b;i++){
		int s,t;scanf("%d%d",&s,&t);s--;t--;r(s,t);
	}
	int sz=0;
	for(int i=0;i<a;i++){
		if(p[i]<0)u[sz++]=-p[i];
	}
	int vs=0;
	std::sort(u,u+sz);
	for(int i=0;i<sz;i++){
		if(u[i]<=SQ)cnt[u[i]]++;
		else v[vs++]=u[i];
	}
	for(int i=0;i<=SQ;i++)for(int j=0;j<=a;j++)dp[i][j]=99999999;
	dp[0][0]=0;
	for(int i=1;i<=SQ;i++){
		if(cnt[i]==0){
			for(int j=0;j<=a;j++){
				dp[i][j]=dp[i-1][j];
			}continue;
		}
		int lim=cnt[i];
		for(int j=0;j<i;j++){
			deque<pair<int,int> > Q;
			for(int k=j;k<=a;k+=i){
				if(dp[i-1][k]<9999999){
					while(Q.size()&&dp[i-1][k]-k/i<Q.back().first){
						Q.pop_back();
					}
					Q.push_back(make_pair(dp[i-1][k]-k/i,k/i));
				}
				if(Q.size()){
					dp[i][k]=Q.front().first+k/i;
				}
				while(Q.size()&&Q.front().second==k/i-lim){
					Q.pop_front();
				}
			}
		}
	}
	for(int i=0;i<=vs;i++)for(int j=0;j<=a;j++)dp2[i][j]=99999999;
	for(int i=0;i<=a;i++)dp2[0][i]=dp[SQ][i];
	for(int i=0;i<vs;i++){
		for(int j=0;j<=a;j++){
			dp2[i+1][j]=min(dp2[i+1][j],dp2[i][j]);
			if(j+v[i]<=a)dp2[i+1][j+v[i]]=min(dp2[i+1][j+v[i]],dp2[i][j]+1);
		}
	}
	for(int i=1;i<=a;i++){
		if(dp2[vs][i]>999999)printf("-1\n");
		else printf("%d\n",dp2[vs][i]-1);
	}
}
0