結果

問題 No.317 辺の追加
ユーザー cielciel
提出日時 2015-12-10 15:28:21
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,259 bytes
コンパイル時間 628 ms
コンパイル使用メモリ 76,332 KB
実行使用メモリ 8,256 KB
最終ジャッジ日時 2023-10-13 10:28:29
合計ジャッジ時間 7,502 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 639 ms
5,812 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 854 ms
8,256 KB
testcase_12 WA -
testcase_13 AC 1,231 ms
7,344 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 342 ms
5,284 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:47:14: warning: ‘tbelow’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   else tbelow+=e;
        ~~~~~~^~~

ソースコード

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;}
	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;
	}
	void enumerate(vector<int> &edges){
		unordered_map<int,int> r;
		for(int i=0;i<n;i++)r[root(i)]++;
		for(auto &e:r)edges.push_back(e.second);
		sort(edges.begin(),edges.end());
	}
};
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);
	}
	vector<int>edges;
	uf.enumerate(edges);
	vector<int>bag(N+1,INF);
	bag[0]=-1;
	int t=0,tbelow,prev=0;
	for(auto &e:edges){
		if(prev!=e)prev=e,tbelow=e;
		else tbelow+=e;
		t+=e;
		for(int i=t;i>=tbelow;i--)if(bag[i-e]<INF){
			bag[i]=min(bag[i],bag[i-e]+1);
		}
	}
	for(int i=1;i<=N;i++)printf("%d\n",bag[i]);
}
0