結果

問題 No.317 辺の追加
ユーザー 👑 kmjpkmjp
提出日時 2015-12-10 00:47:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 204 ms / 2,000 ms
コード長 2,139 bytes
コンパイル時間 2,209 ms
コンパイル使用メモリ 163,884 KB
実行使用メモリ 10,788 KB
最終ジャッジ日時 2023-10-13 09:52:20
合計ジャッジ時間 6,610 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
8,824 KB
testcase_01 AC 7 ms
8,608 KB
testcase_02 AC 44 ms
9,680 KB
testcase_03 AC 45 ms
9,576 KB
testcase_04 AC 87 ms
9,844 KB
testcase_05 AC 45 ms
9,404 KB
testcase_06 AC 55 ms
9,408 KB
testcase_07 AC 22 ms
9,316 KB
testcase_08 AC 204 ms
10,572 KB
testcase_09 AC 58 ms
9,988 KB
testcase_10 AC 64 ms
9,624 KB
testcase_11 AC 79 ms
10,372 KB
testcase_12 AC 64 ms
9,668 KB
testcase_13 AC 115 ms
10,788 KB
testcase_14 AC 57 ms
9,632 KB
testcase_15 AC 62 ms
9,584 KB
testcase_16 AC 83 ms
10,432 KB
testcase_17 AC 60 ms
9,684 KB
testcase_18 AC 64 ms
9,588 KB
testcase_19 AC 65 ms
9,660 KB
testcase_20 AC 115 ms
10,108 KB
testcase_21 AC 21 ms
9,408 KB
testcase_22 AC 15 ms
9,048 KB
testcase_23 AC 15 ms
9,104 KB
testcase_24 AC 32 ms
9,644 KB
testcase_25 AC 24 ms
9,820 KB
testcase_26 AC 25 ms
9,576 KB
testcase_27 AC 22 ms
9,368 KB
testcase_28 AC 15 ms
9,104 KB
testcase_29 AC 8 ms
9,108 KB
testcase_30 AC 75 ms
9,324 KB
testcase_31 AC 67 ms
9,364 KB
testcase_32 AC 67 ms
9,312 KB
testcase_33 AC 67 ms
9,432 KB
testcase_34 AC 66 ms
9,308 KB
testcase_35 AC 66 ms
9,468 KB
testcase_36 AC 65 ms
9,324 KB
testcase_37 AC 66 ms
9,320 KB
testcase_38 AC 73 ms
9,420 KB
testcase_39 AC 64 ms
9,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M;

template<int um> class UF {
	public:
	vector<int> par,rank,cnt;
	UF() {par=rank=vector<int>(um,0); cnt=vector<int>(um,1); for(int i=0;i<um;i++) par[i]=i;}
	void reinit() {int i; FOR(i,um) rank[i]=0,cnt[i]=1,par[i]=i;}
	int operator[](int x) {return (par[x]==x)?(x):(par[x] = operator[](par[x]));}
	int count(int x) { return cnt[operator[](x)];}
	int operator()(int x,int y) {
		if((x=operator[](x))==(y=operator[](y))) return x;
		cnt[y]=cnt[x]=cnt[x]+cnt[y];
		if(rank[x]>rank[y]) return par[x]=y;
		rank[x]+=rank[x]==rank[y]; return par[y]=x;
	}
};
UF<500000> uf;
map<int,int> E;
int mi[101010],pre[101010];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M;
	FOR(i,M) {
		cin>>x>>y;
		uf(x-1,y-1);
	}
	FOR(i,N) if(uf[i]==i) E[uf.cnt[i]]++;
	FOR(i,N+1) mi[i]=N+100;
	mi[0]=0;
	
	FORR(r,E) {
		if(r.first>=100 || r.second<=10) {
			FOR(j,r.second) {
				for(i=N-r.first+1;i>=0;i--) mi[i+r.first]=min(mi[i+r.first],mi[i]+1);
			}
		}
		else {
			set<pair<int,int>> S[100];
			FOR(i,100) S[i].clear();
			
			S[0].insert({0,0});
			for(i=j=1;i<=N;i++,j++) {
				pre[i]=mi[i];
				if(j>=r.first) j=0;
				auto it=S[j].begin();
				
				if(it!=S[j].end()) {
					x=it->first+(it->second-j)/r.first+(i-it->second)/r.first;
					mi[i]=min(mi[i],x);
				}
				if(pre[i]<N) S[j].insert({pre[i]-(i-j)/r.first,i});
				x=i-r.first*r.second;
				if(x>=0) S[j].erase({pre[x]-(x-j)/r.first,x});
			}
			
		}
	}
	
	for(i=1;i<=N;i++) {
		if(mi[i]>N) mi[i]=0;
		_P("%d\n",mi[i]-1);
	}
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0