結果

問題 No.317 辺の追加
ユーザー koyumeishikoyumeishi
提出日時 2015-12-10 00:32:39
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,980 bytes
コンパイル時間 785 ms
コンパイル使用メモリ 89,792 KB
実行使用メモリ 4,480 KB
最終ジャッジ日時 2023-10-13 09:48:39
合計ジャッジ時間 5,622 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,480 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
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 <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 <bitset>
typedef bitset<100000> bits;

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

	//map<int,int> w;
	vector<int> unko;
	for(int i=0; i<n; i++){
		//if(cnt[i]) w[cnt[i]] += 1;
		if(cnt[i]) unko.push_back(cnt[i]);
	}
	sort(unko.begin(), unko.end());

	vector<bits> x(unko.size());
	for(int i=0; i<unko.size(); i++){
		for(int k=i; k>0; k--){
			x[k] |= x[k-1]<<unko[i];
		}
		x[0].set(unko[i]);
	}
	for(int i=1; i<unko.size(); i++){
		x[i] |= x[i-1];
	}

	vector<int> ans(n+1, -1);
	for(int i=1; i<=n; i++){
		auto check = [&](int pos){
			return x[pos][i];
		};
		long long lb = -1;
		long long ub = unko.size()-1;
		while(ub-lb>1){
			long long mid = (lb+ub)/2;
			bool ok = check(mid);
			(ok? ub : lb) = mid;
		}
		/*
		if(check[ub]){
			ans[i] = ub;
		}
		*/
		printf("%d\n", check(ub)?ub:-1);
	}

	return 0;
}
0