結果

問題 No.317 辺の追加
ユーザー TawaraTawara
提出日時 2015-12-12 15:58:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 1,496 bytes
コンパイル時間 941 ms
コンパイル使用メモリ 85,992 KB
実行使用メモリ 8,132 KB
最終ジャッジ日時 2023-10-13 11:47:20
合計ジャッジ時間 9,540 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 160 ms
4,348 KB
testcase_03 AC 158 ms
4,352 KB
testcase_04 AC 166 ms
5,432 KB
testcase_05 AC 141 ms
4,356 KB
testcase_06 AC 183 ms
4,352 KB
testcase_07 AC 58 ms
4,348 KB
testcase_08 AC 200 ms
5,900 KB
testcase_09 AC 196 ms
4,360 KB
testcase_10 AC 229 ms
4,348 KB
testcase_11 AC 158 ms
8,132 KB
testcase_12 AC 230 ms
4,368 KB
testcase_13 AC 172 ms
7,472 KB
testcase_14 AC 205 ms
4,348 KB
testcase_15 AC 227 ms
4,372 KB
testcase_16 AC 189 ms
5,112 KB
testcase_17 AC 212 ms
4,348 KB
testcase_18 AC 227 ms
4,348 KB
testcase_19 AC 232 ms
4,352 KB
testcase_20 AC 189 ms
5,424 KB
testcase_21 AC 90 ms
4,348 KB
testcase_22 AC 50 ms
4,348 KB
testcase_23 AC 50 ms
4,348 KB
testcase_24 AC 157 ms
4,376 KB
testcase_25 AC 113 ms
4,532 KB
testcase_26 AC 120 ms
4,372 KB
testcase_27 AC 94 ms
4,352 KB
testcase_28 AC 50 ms
4,348 KB
testcase_29 AC 13 ms
4,352 KB
testcase_30 AC 203 ms
4,352 KB
testcase_31 AC 200 ms
4,352 KB
testcase_32 AC 200 ms
4,352 KB
testcase_33 AC 202 ms
4,348 KB
testcase_34 AC 200 ms
4,348 KB
testcase_35 AC 200 ms
4,352 KB
testcase_36 AC 200 ms
4,352 KB
testcase_37 AC 200 ms
4,352 KB
testcase_38 AC 202 ms
4,356 KB
testcase_39 AC 196 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <cmath>
#include <string>

using namespace std;

typedef vector <int> VI;
typedef vector <VI> VVI;
typedef vector <string> VS;
typedef pair<int,int> PII;
typedef long long LL;
typedef vector <LL> VLL;
typedef unsigned long long ULL;
typedef pair <int, LL> PIL;
typedef vector <PIL> VPIL;

//#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
//#define sort(c) sort((c).begin(),(c).end())

#define range(i,a,b) for(int i=(a); i < (b); i++)
#define rep(i,n) range(i,0,n)

int parent[100001];

int root(int x){
	if (x != parent[x]) parent[x] = root(parent[x]);
	return parent[x];
}
int main(){
	int N,M,u,v,w,num,mul;
	map <int,int> conn_comp, comp_num;
	map <int,int> :: iterator mit;
	VI ans;
	cin >> N >> M;
	ans = VI(N+1,N); ans[0] = 0;
	rep(i,N) parent[i] = i;
	rep(i,M){
		cin >> u >> v;
		u = root(u-1); v = root(v-1);
		if (u!=v) parent[u] = v;
	}
	rep(i,N) conn_comp[root(i)]++;
	for (mit=conn_comp.begin();mit!=conn_comp.end();mit++) comp_num[mit->second]++;
	for (mit=comp_num.begin();mit!=comp_num.end();mit++){
		num = mit->second;
		for (int i = 1; num > 0; i<<=1){
			mul = min(i,num);
			w = mul*mit->first;
			for (int j = N; j >= w; j--) ans[j] = min(ans[j],ans[j-w]+mul);
			num -= mul;
		}
	}
	rep(i,N) cout << (ans[i+1]==N ? -1 : ans[i+1]-1) << endl;
	return 0;
}
0