結果

問題 No.317 辺の追加
ユーザー TawaraTawara
提出日時 2015-12-13 14:52:42
言語 Python2
(2.7.18)
結果
TLE  
実行時間 -
コード長 811 bytes
コンパイル時間 543 ms
コンパイル使用メモリ 6,740 KB
実行使用メモリ 10,896 KB
最終ジャッジ日時 2023-10-13 14:56:27
合計ジャッジ時間 8,245 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
9,472 KB
testcase_01 AC 18 ms
9,412 KB
testcase_02 AC 1,578 ms
10,896 KB
testcase_03 AC 888 ms
9,864 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from math import log
parent = range(100000)
def root(x):
	while x != parent[x]: parent[x] = x = parent[parent[x]]
	return x
N,M = map(int,raw_input().split())
conn_comp = defaultdict(int)
comp_num = defaultdict(int)
ans = [N]*(N+1)
ans[0] = 0
for i in xrange(M):
	u,v = map(lambda x:root(int(x)-1),raw_input().split())
	parent[u] = v
for i in xrange(N): conn_comp[root(i)] += 1
for v in conn_comp.itervalues(): comp_num[v] += 1
for v_num,num in comp_num.iteritems():
	log_num = int(log(num,2))
	mul = 1
	for i in xrange(log_num):
		w = v_num*mul
		for j in xrange(N,w-1,-1): ans[j] = min(ans[j],ans[j-w]+mul)
		num -= mul
		mul *= 2
	if num > 0:
		w = v_num*num
		for j in xrange(N,w-1,-1): ans[j] = min(ans[j],ans[j-w]+num)
for i in xrange(1,N+1):
	print ans[i]*(ans[i]<N)-1
0