結果

問題 No.317 辺の追加
ユーザー cielciel
提出日時 2015-12-10 00:50:17
言語 C++11
(gcc 11.4.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,229 bytes
コンパイル時間 695 ms
コンパイル使用メモリ 72,616 KB
実行使用メモリ 8,336 KB
最終ジャッジ日時 2023-10-13 09:54:14
合計ジャッジ時間 10,598 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 48 ms
4,348 KB
testcase_03 AC 35 ms
4,348 KB
testcase_04 AC 400 ms
4,912 KB
testcase_05 AC 36 ms
4,348 KB
testcase_06 AC 44 ms
4,352 KB
testcase_07 AC 15 ms
4,348 KB
testcase_08 AC 1,140 ms
5,884 KB
testcase_09 AC 76 ms
4,348 KB
testcase_10 AC 51 ms
4,348 KB
testcase_11 AC 1,530 ms
8,336 KB
testcase_12 AC 51 ms
4,348 KB
testcase_13 TLE -
testcase_14 AC 49 ms
4,352 KB
testcase_15 AC 47 ms
4,348 KB
testcase_16 AC 338 ms
4,976 KB
testcase_17 AC 48 ms
4,348 KB
testcase_18 AC 48 ms
4,348 KB
testcase_19 AC 48 ms
4,352 KB
testcase_20 AC 583 ms
5,424 KB
testcase_21 AC 15 ms
4,352 KB
testcase_22 AC 9 ms
4,348 KB
testcase_23 AC 9 ms
4,348 KB
testcase_24 AC 25 ms
4,348 KB
testcase_25 AC 18 ms
4,352 KB
testcase_26 AC 19 ms
4,348 KB
testcase_27 AC 15 ms
4,348 KB
testcase_28 AC 9 ms
4,348 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 59 ms
4,348 KB
testcase_31 AC 53 ms
4,348 KB
testcase_32 AC 54 ms
4,352 KB
testcase_33 AC 53 ms
4,352 KB
testcase_34 AC 55 ms
4,352 KB
testcase_35 AC 56 ms
4,352 KB
testcase_36 AC 54 ms
4,352 KB
testcase_37 AC 56 ms
4,352 KB
testcase_38 AC 63 ms
4,348 KB
testcase_39 AC 56 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:45:14: warning: ‘tbelow’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   else tbelow+=e;
        ~~~~~~^~~

ソースコード

diff #

#include <vector>
#include <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){
		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(){
	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,-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(i==e||bag[i-e]>=0){
			if(bag[i]<0||bag[i]>bag[i-e]+1)bag[i]=bag[i-e]+1;
		}
	}
	for(int i=1;i<=N;i++)printf("%d\n",bag[i]);
}
0