結果

問題 No.317 辺の追加
ユーザー NekosyndromeNekosyndrome
提出日時 2015-12-11 20:48:08
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,199 bytes
コンパイル時間 1,767 ms
コンパイル使用メモリ 144,372 KB
実行使用メモリ 5,604 KB
最終ジャッジ日時 2023-10-13 11:24:39
合計ジャッジ時間 4,585 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
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 -
権限があれば一括ダウンロードができます

ソースコード

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)
{
	printf("%d %d\n",x,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