結果

問題 No.317 辺の追加
ユーザー NekosyndromeNekosyndrome
提出日時 2015-12-11 20:48:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 1,175 bytes
コンパイル時間 1,081 ms
コンパイル使用メモリ 145,148 KB
実行使用メモリ 5,552 KB
最終ジャッジ日時 2023-10-13 11:24:44
合計ジャッジ時間 5,019 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 33 ms
4,772 KB
testcase_03 AC 43 ms
4,544 KB
testcase_04 AC 35 ms
5,004 KB
testcase_05 AC 42 ms
4,352 KB
testcase_06 AC 49 ms
4,632 KB
testcase_07 AC 17 ms
4,352 KB
testcase_08 AC 43 ms
5,268 KB
testcase_09 AC 43 ms
5,260 KB
testcase_10 AC 60 ms
5,000 KB
testcase_11 AC 18 ms
5,000 KB
testcase_12 AC 58 ms
5,092 KB
testcase_13 AC 22 ms
5,028 KB
testcase_14 AC 44 ms
5,088 KB
testcase_15 AC 53 ms
5,080 KB
testcase_16 AC 36 ms
5,088 KB
testcase_17 AC 51 ms
5,092 KB
testcase_18 AC 56 ms
5,132 KB
testcase_19 AC 56 ms
5,000 KB
testcase_20 AC 38 ms
5,132 KB
testcase_21 AC 15 ms
4,688 KB
testcase_22 AC 10 ms
4,348 KB
testcase_23 AC 10 ms
4,352 KB
testcase_24 AC 28 ms
5,552 KB
testcase_25 AC 19 ms
5,256 KB
testcase_26 AC 21 ms
5,220 KB
testcase_27 AC 17 ms
4,888 KB
testcase_28 AC 9 ms
4,348 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 55 ms
5,248 KB
testcase_31 AC 52 ms
5,016 KB
testcase_32 AC 51 ms
5,152 KB
testcase_33 AC 52 ms
5,096 KB
testcase_34 AC 51 ms
5,096 KB
testcase_35 AC 54 ms
5,136 KB
testcase_36 AC 56 ms
5,032 KB
testcase_37 AC 52 ms
5,140 KB
testcase_38 AC 57 ms
5,024 KB
testcase_39 AC 53 ms
5,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define REP(x,y,z) for(int x=y;x<=z;x++)
#define FORD(x,y,z) for(int x=y;x>=z;x--)
#define MSET(x,y) memset(x,y,sizeof(x))
#define FOR(x,y) for(__typeof(y.begin()) x=y.begin();x!=y.end();x++)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 100005
#define INF 1000000000
void RI(){}
template<typename... T>
void RI( int& head, T&... tail ) {
    scanf("%d",&head);
    RI(tail...);
}
using namespace std;
typedef long long LL;
int n,m,fa[M],w[M],cnt[M],dp[M];
int find(int x)
{
	return fa[x]==x ? x:fa[x]=find(fa[x]);
}
void con(int x,int y)
{
	x=find(x);
	y=find(y);
	if(x==y) return;
	fa[x] = y;
	w[y] += w[x];
}
void upd(int x,int y)
{
	FORD(i,n,x) dp[i]=min(dp[i], dp[i-x]+y);
}
int main()
{
	int x,y;
	while(~scanf("%d %d",&n,&m))
	{
		dp[0]=0;
		REP(i,1,n) fa[i]=i, w[i]=1, cnt[i]=0, dp[i]=INF;
		REP(i,1,m)
		{
			RI(x,y);
			con(x,y);
		}

		REP(i,1,n) if(find(i)==i) cnt[w[i]]++;
		REP(i,1,n) if(cnt[i])
		{
			x = cnt[i];
			y = 1;
			while(x>=y)
			{
				upd(i*y, y);
				x-=y;
				y*=2;
			}
			if(x) upd(i*x, x);
		}
		REP(i,1,n) printf("%d\n",dp[i]==INF?-1:dp[i]-1);
	}
	return 0;
}
0