結果

問題 No.317 辺の追加
ユーザー koyumeishikoyumeishi
提出日時 2015-12-10 02:51:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 1,684 bytes
コンパイル時間 656 ms
コンパイル使用メモリ 79,020 KB
実行使用メモリ 5,128 KB
最終ジャッジ日時 2023-10-13 10:15:19
合計ジャッジ時間 4,742 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 34 ms
4,600 KB
testcase_03 AC 38 ms
4,372 KB
testcase_04 AC 36 ms
4,896 KB
testcase_05 AC 39 ms
4,348 KB
testcase_06 AC 47 ms
4,352 KB
testcase_07 AC 17 ms
4,352 KB
testcase_08 AC 53 ms
5,020 KB
testcase_09 AC 42 ms
4,896 KB
testcase_10 AC 54 ms
4,944 KB
testcase_11 AC 20 ms
4,952 KB
testcase_12 AC 54 ms
4,980 KB
testcase_13 AC 28 ms
4,868 KB
testcase_14 AC 46 ms
4,828 KB
testcase_15 AC 51 ms
5,000 KB
testcase_16 AC 40 ms
5,004 KB
testcase_17 AC 48 ms
5,016 KB
testcase_18 AC 55 ms
4,944 KB
testcase_19 AC 56 ms
4,836 KB
testcase_20 AC 43 ms
5,128 KB
testcase_21 AC 16 ms
4,348 KB
testcase_22 AC 9 ms
4,352 KB
testcase_23 AC 9 ms
4,348 KB
testcase_24 AC 27 ms
4,816 KB
testcase_25 AC 19 ms
4,372 KB
testcase_26 AC 21 ms
4,424 KB
testcase_27 AC 17 ms
4,376 KB
testcase_28 AC 10 ms
4,356 KB
testcase_29 AC 3 ms
4,352 KB
testcase_30 AC 64 ms
4,976 KB
testcase_31 AC 59 ms
5,012 KB
testcase_32 AC 58 ms
5,016 KB
testcase_33 AC 59 ms
4,936 KB
testcase_34 AC 59 ms
4,936 KB
testcase_35 AC 59 ms
5,008 KB
testcase_36 AC 59 ms
4,932 KB
testcase_37 AC 59 ms
4,868 KB
testcase_38 AC 63 ms
5,000 KB
testcase_39 AC 59 ms
5,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

class UnionFindTree{
	typedef struct {
		int parent;
		int rank;
	}base_node;
	
	vector<base_node> node;
public:
	UnionFindTree(int n){
		node.resize(n);
		for(int i=0; i<n; i++){
			node[i].parent=i;
			node[i].rank=0;
		}
	}

	int find(int x){
		if(node[x].parent == x) return x;
		else{
			return node[x].parent = find(node[x].parent);
		}
	}
	
	bool same(int x, int y){
		return find(x) == find(y);
	}

	void unite(int x, int y){
		x = find(node[x].parent);
		y = find(node[y].parent);
		if(x==y) return;
		if(node[x].rank < node[y].rank){
			node[x].parent = y;
		}else if(node[x].rank > node[y].rank){
			node[y].parent = x;
		}else{
			node[x].rank++;
			unite(x,y);
		}
	}
};

#include <cassert>

int main(){
	int n,m;
	//cin >> n >> m;
	scanf("%d%d", &n, &m);
	UnionFindTree uft(n);

	for(int i=0; i<m; ++i){
		int u,v;
		scanf("%d%d", &u, &v);
		u--;v--;
		uft.unite(u,v);
	}

	vector<int> cnt(n, 0);
	for(int i=0; i<n; ++i){
		int r = uft.find(i);
		cnt[r]++;
	}

	vector<int> unko(n+1, 0);
	for(int i=0; i<n; ++i){
		if(cnt[i]) unko[cnt[i]] += 1;
	}

	const int inf = 5*n;
	vector<int> ans(n+1, inf);
	ans[0] =-1;

	for(int i=0; i<=n; ++i){
		for(int k=1; unko[i]; k<<=1){
			int c = min(unko[i], k);
			for(int p=n-c * i; p>=0; --p){
				if(ans[p] == inf) continue;
				if(ans[p + c * i] > ans[p] + c){
					ans[p+c * i] = ans[p] + c;
				}
			}
			unko[i] -= c;
		}
	}
	for(int i=1; i<=n; i++){
		if(ans[i] == inf) ans[i] = -1;
		printf("%d\n", ans[i]);
	}

	return 0;
}
0