結果

問題 No.317 辺の追加
ユーザー koyumeishikoyumeishi
提出日時 2015-12-10 02:48:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 1,732 bytes
コンパイル時間 665 ms
コンパイル使用メモリ 81,688 KB
実行使用メモリ 11,460 KB
最終ジャッジ日時 2023-10-13 10:15:06
合計ジャッジ時間 5,972 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 64 ms
8,856 KB
testcase_03 AC 72 ms
8,832 KB
testcase_04 AC 67 ms
8,884 KB
testcase_05 AC 69 ms
7,864 KB
testcase_06 AC 91 ms
9,316 KB
testcase_07 AC 25 ms
5,224 KB
testcase_08 AC 101 ms
9,588 KB
testcase_09 AC 81 ms
10,208 KB
testcase_10 AC 117 ms
11,164 KB
testcase_11 AC 33 ms
7,968 KB
testcase_12 AC 113 ms
11,388 KB
testcase_13 AC 46 ms
8,436 KB
testcase_14 AC 90 ms
10,736 KB
testcase_15 AC 99 ms
11,360 KB
testcase_16 AC 73 ms
10,020 KB
testcase_17 AC 96 ms
10,700 KB
testcase_18 AC 110 ms
11,232 KB
testcase_19 AC 120 ms
11,460 KB
testcase_20 AC 77 ms
9,652 KB
testcase_21 AC 23 ms
6,928 KB
testcase_22 AC 13 ms
5,220 KB
testcase_23 AC 13 ms
5,156 KB
testcase_24 AC 38 ms
9,640 KB
testcase_25 AC 28 ms
7,916 KB
testcase_26 AC 29 ms
8,068 KB
testcase_27 AC 24 ms
7,068 KB
testcase_28 AC 14 ms
5,088 KB
testcase_29 AC 4 ms
4,352 KB
testcase_30 AC 103 ms
10,432 KB
testcase_31 AC 94 ms
10,440 KB
testcase_32 AC 95 ms
10,500 KB
testcase_33 AC 96 ms
10,372 KB
testcase_34 AC 94 ms
10,496 KB
testcase_35 AC 96 ms
10,496 KB
testcase_36 AC 94 ms
10,468 KB
testcase_37 AC 96 ms
10,504 KB
testcase_38 AC 104 ms
10,448 KB
testcase_39 AC 96 ms
10,652 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;
	vector<vector<int>> g(n);

	UnionFindTree uft(n);

	for(int i=0; i<m; i++){
		int u,v;
		scanf("%d%d", &u, &v);
		u--;v--;
		g[u].push_back(v);
		g[v].push_back(u);

		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;
	}

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

	for(int i=0; i<=n; ++i){
		for(int k=1; unko[i]>0; k<<=1){
			int c = min(unko[i], k);
			vector<int> next;

			for(int p=n-c * i; p>=0; --p){
				if(ans[p] == 5*n) 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] == 5*n) ans[i] = -1;
		printf("%d\n", ans[i]);
	}

	return 0;
}
0