結果

問題 No.317 辺の追加
ユーザー tailstails
提出日時 2015-12-10 23:48:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 356 ms / 2,000 ms
コード長 874 bytes
コンパイル時間 1,316 ms
コンパイル使用メモリ 146,680 KB
実行使用メモリ 19,916 KB
最終ジャッジ日時 2023-10-13 10:42:10
合計ジャッジ時間 11,276 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,708 KB
testcase_01 AC 3 ms
5,704 KB
testcase_02 AC 200 ms
12,956 KB
testcase_03 AC 228 ms
15,760 KB
testcase_04 AC 182 ms
10,296 KB
testcase_05 AC 223 ms
16,656 KB
testcase_06 AC 286 ms
18,744 KB
testcase_07 AC 80 ms
9,928 KB
testcase_08 AC 199 ms
9,712 KB
testcase_09 AC 258 ms
14,404 KB
testcase_10 AC 343 ms
19,616 KB
testcase_11 AC 142 ms
6,856 KB
testcase_12 AC 350 ms
19,624 KB
testcase_13 AC 156 ms
7,516 KB
testcase_14 AC 282 ms
16,536 KB
testcase_15 AC 326 ms
18,580 KB
testcase_16 AC 220 ms
11,528 KB
testcase_17 AC 306 ms
17,184 KB
testcase_18 AC 346 ms
19,728 KB
testcase_19 AC 356 ms
19,916 KB
testcase_20 AC 206 ms
10,656 KB
testcase_21 AC 101 ms
9,960 KB
testcase_22 AC 55 ms
7,824 KB
testcase_23 AC 56 ms
7,956 KB
testcase_24 AC 174 ms
12,924 KB
testcase_25 AC 129 ms
11,572 KB
testcase_26 AC 131 ms
11,588 KB
testcase_27 AC 104 ms
10,420 KB
testcase_28 AC 57 ms
8,096 KB
testcase_29 AC 16 ms
6,352 KB
testcase_30 AC 205 ms
12,424 KB
testcase_31 AC 205 ms
12,556 KB
testcase_32 AC 204 ms
12,484 KB
testcase_33 AC 205 ms
12,500 KB
testcase_34 AC 204 ms
12,512 KB
testcase_35 AC 206 ms
12,440 KB
testcase_36 AC 203 ms
12,436 KB
testcase_37 AC 203 ms
12,440 KB
testcase_38 AC 205 ms
12,552 KB
testcase_39 AC 204 ms
12,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define Nmax 100010
bool used[Nmax];
list<int> edges[Nmax];
int cs[Nmax];
int dp[Nmax];

int countf(int i){
	if(used[i]) return 0;
	used[i]=true;
	int result = 1;
	for(auto it=edges[i].begin(); it!=edges[i].end(); ++it){
		result += countf(*it);
	}
	return result;
}

int main(){
	int N,M;
	cin>>N>>M;

	for(int j=0;j<M;++j){
		int u,v;
		cin>>u>>v;
		edges[u].push_back(v);
		edges[v].push_back(u);
	}
	
	dp[0]=-1;
	for(int k=1;k<=N;++k){
		dp[k]=N;
	}

	for(int i=1;i<=N;++i){
		int c = countf(i);
		++cs[c];
	}

	int sum=0;
	for(int c=1;c<=N;++c){
		int s=cs[c];
		int d=1;
		while(s){
			int e=d;
			if(e>s)e=s;
			s-=e;
			int ce=c*e;
			sum+=ce;
			for(int k=sum-ce;k>=0;--k){
				if(dp[k+ce]>e+dp[k]){
					dp[k+ce]=e+dp[k];
				}
			}
			d*=2;
		}
	}

	for(int k=1;k<=N;++k){
		cout << (dp[k]<N?dp[k]:-1) << endl;
	}
}
0