結果

問題 No.317 辺の追加
ユーザー cielciel
提出日時 2015-12-10 00:29:33
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,075 bytes
コンパイル時間 481 ms
コンパイル使用メモリ 62,260 KB
実行使用メモリ 8,704 KB
最終ジャッジ日時 2023-10-13 09:47:31
合計ジャッジ時間 5,683 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,704 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 985 ms
4,348 KB
testcase_03 AC 83 ms
4,348 KB
testcase_04 TLE -
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 <vector>
#include <map>
#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;}
	int same(int a,int b){return root(a)==root(b);}
	int unite(int a,int b){
		int x=root(a),y=root(b);if(x==y)return 0;
		if(rank[x]<rank[y]){
			parent[x]=y;
		}else{
			parent[y]=x;
			if(rank[x]==rank[y])rank[x]++;
		}
		return 1;
	}
	map<int,int> enumerate(){
		map<int,int> r;
		for(int i=0;i<n;i++)r[root(i)]++;
		return r;
	}
};
int main(){
	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);
	}
	vector<int>bag(N+1,-1);
	int t=0;
	for(auto &e:uf.enumerate()){
		t+=e.second;
		for(int i=t;i>=e.second;i--)if(i==e.second||bag[i-e.second]>=0){
			if(bag[i]<0||bag[i]>bag[i-e.second]+1)bag[i]=bag[i-e.second]+1;
		}
	}
	for(int i=1;i<=N;i++)printf("%d\n",bag[i]);
}
0