結果

問題 No.317 辺の追加
ユーザー tailstails
提出日時 2015-12-10 13:55:18
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 731 bytes
コンパイル時間 1,180 ms
コンパイル使用メモリ 146,792 KB
実行使用メモリ 12,532 KB
最終ジャッジ日時 2023-10-13 10:24:38
合計ジャッジ時間 9,913 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
10,076 KB
testcase_01 AC 4 ms
5,712 KB
testcase_02 AC 999 ms
9,496 KB
testcase_03 AC 287 ms
10,048 KB
testcase_04 TLE -
testcase_05 AC 186 ms
9,812 KB
testcase_06 AC 260 ms
10,900 KB
testcase_07 AC 73 ms
7,396 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define Nmax 100010
bool used[Nmax];
vector<int> edges[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;
	}

	int sum=0;
	for(int i=1;i<=N;++i){
		int c = countf(i);
		if(c>0){
			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