結果

問題 No.317 辺の追加
ユーザー koyumeishikoyumeishi
提出日時 2015-12-10 02:44:49
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,502 bytes
コンパイル時間 2,404 ms
コンパイル使用メモリ 96,240 KB
実行使用メモリ 11,316 KB
最終ジャッジ日時 2023-10-13 10:14:38
合計ジャッジ時間 10,640 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 WA -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 WA -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 WA -
testcase_32 WA -
testcase_33 RE -
testcase_34 WA -
testcase_35 WA -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
権限があれば一括ダウンロードができます

ソースコード

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

	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.rbegin(), unko.rend());

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

	for(auto itr=w.begin(); itr!=w.end(); itr++){
		for(int k=1; itr->second>0; k<<=1){
			int c = min(itr->second, k);
			vector<int> next;

			for(int p=0; p<=n; p++){
				if(ans[p] == 5*n) continue;

				if(ans[p + c * itr->first] > ans[p] + c){
					ans[p+c * itr->first] = ans[p] + c;
				}
			}
			itr->second -= c;
		}
	}

	/*
	//for(int i=0; i<unko.size(); i++){
	for(auto itr=w.rbegin(); itr!=w.rend(); itr++){
		vector<int> next;
		while(pos.size()){
			int p = pos.front();
			pos.pop();
			next.push_back(p);
			for(int k=0; k<itr->second; k++){
				if(ans[p + itr->first] > ans[p] + 1){
					ans[p + itr->first] = ans[p] + 1;
					next.push_back(p + itr->first);
					p += itr->first;
				}
			}
		}
		sort(next.begin(), next.end());
		next.erase( unique(next.begin(), next.end()), next.end());
		for(auto itr_=next.rbegin(); itr_!=next.rend(); itr_++){
			pos.push(*itr_);
		}
	}
	*/
	for(int i=1; i<=n; i++){
		if(ans[i] == 5*n) ans[i] = -1;
		printf("%d\n", ans[i]);
	}

	return 0;
}
0