結果

問題 No.317 辺の追加
ユーザー cielciel
提出日時 2015-12-11 05:37:23
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,370 bytes
コンパイル時間 585 ms
コンパイル使用メモリ 73,372 KB
実行使用メモリ 8,112 KB
最終ジャッジ日時 2023-10-13 10:52:34
合計ジャッジ時間 4,306 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 15 ms
4,352 KB
testcase_22 AC 9 ms
4,352 KB
testcase_23 WA -
testcase_24 AC 26 ms
4,352 KB
testcase_25 AC 20 ms
4,348 KB
testcase_26 AC 21 ms
4,352 KB
testcase_27 AC 17 ms
4,352 KB
testcase_28 AC 10 ms
4,348 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 WA -
testcase_31 AC 55 ms
4,348 KB
testcase_32 AC 55 ms
4,348 KB
testcase_33 AC 55 ms
4,352 KB
testcase_34 AC 55 ms
4,348 KB
testcase_35 AC 55 ms
4,352 KB
testcase_36 AC 56 ms
4,352 KB
testcase_37 AC 56 ms
4,352 KB
testcase_38 WA -
testcase_39 AC 54 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <unordered_map>
#include <algorithm>
#include <cstdio>
using namespace std;
class union_find{
	int *parent,*rank,n;
public:
	int root(int a){return parent[a]==a?a:(parent[a]=root(parent[a]));}
	union_find(int _n){n=_n;parent=new int[n];rank=new int[n];for(int i=0;i<n;i++)parent[i]=i,rank[i]=0;}
	~union_find(){delete []parent;delete []rank;}
	void unite(int a,int b){
		int x=root(a),y=root(b);
		if(rank[x]<rank[y]){
			parent[x]=y;
		}else{
			parent[y]=x;
			if(rank[x]==rank[y])rank[x]++;
		}
	}
	void enumerate(unordered_map<int,int> &edges){
		unordered_map<int,int> r;
		for(int i=0;i<n;i++)++r[root(i)];
		for(auto &e:r)edges[e.second]++;
	}
};
int main(){
	const int INF=1<<29;
	int N,M;
	scanf("%d%d",&N,&M);
	union_find uf(N);
	for(;M--;){
		int a,b;
		scanf("%d%d",&a,&b);
		uf.unite(a-1,b-1);
	}
	unordered_map<int,int>edges;
	uf.enumerate(edges);
	vector<int>bag(N+1,N);
	bag[0]=-1;
	int t=0;
	for(auto &e:edges){
		int n=e.second;
#if 1
		for(int d=1;n;d*=2){
			int x=min(d,n);
			n-=x;
			int y=e.first*x;
			t+=y;
			for(int i=t;i>=y;i--)if(bag[i-y]<N){
				bag[i]=min(bag[i],bag[i-y]+x);
			}
			d*=2;
		}
#else
		for(int j=n;j--;){
			t+=e.first;
			for(int i=t;i>=e.first;i--)if(bag[i-e.first]<N){
				bag[i]=min(bag[i],bag[i-e.first]+1);
			}
		}
#endif
	}
	for(int i=1;i<=N;i++)printf("%d\n",bag[i]==N ? -1 : bag[i]);
}
0