結果

問題 No.317 辺の追加
ユーザー tailstails
提出日時 2015-12-10 23:40:54
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 867 bytes
コンパイル時間 1,495 ms
コンパイル使用メモリ 146,800 KB
実行使用メモリ 20,156 KB
最終ジャッジ日時 2023-10-13 10:41:56
合計ジャッジ時間 11,190 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,780 KB
testcase_01 AC 4 ms
5,708 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 102 ms
10,108 KB
testcase_22 AC 55 ms
7,840 KB
testcase_23 WA -
testcase_24 AC 174 ms
12,976 KB
testcase_25 AC 124 ms
11,556 KB
testcase_26 AC 132 ms
11,556 KB
testcase_27 AC 105 ms
10,688 KB
testcase_28 AC 56 ms
8,168 KB
testcase_29 AC 16 ms
6,288 KB
testcase_30 WA -
testcase_31 AC 206 ms
12,420 KB
testcase_32 AC 205 ms
12,484 KB
testcase_33 AC 207 ms
12,700 KB
testcase_34 AC 205 ms
12,416 KB
testcase_35 AC 205 ms
12,700 KB
testcase_36 AC 204 ms
12,548 KB
testcase_37 AC 204 ms
12,464 KB
testcase_38 WA -
testcase_39 AC 206 ms
12,444 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 d=c;
		while(cs[d]>2){
			cs[d]-=2;
			d*=2;
			cs[d]+=1;
		}
	}

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

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