結果

問題 No.317 辺の追加
ユーザー titiatitia
提出日時 2023-09-20 00:09:41
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,262 bytes
コンパイル時間 2,615 ms
コンパイル使用メモリ 87,028 KB
実行使用メモリ 86,068 KB
最終ジャッジ日時 2023-09-20 00:09:50
合計ジャッジ時間 7,066 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
71,884 KB
testcase_01 AC 96 ms
71,548 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
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 #

N,M=map(int,input().split())

from collections import Counter

# UnionFind

Group = [i for i in range(N)] # グループ分け
Nodes = [1]*(N) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

for i in range(M):
    x,y=map(int,input().split())
    x-=1
    y-=1
    Union(x,y)

LIST=[]

for i in range(N):
    if find(i)==i:
        LIST.append(Nodes[i])

C=Counter(LIST)

DP=[1<<30]*(N+1)
DP[0]=0

for l in LIST:
    c=C[l]

    LL=[]

    for j in range(30):
        if c>2**i:
            LL.append(2**i)
            c-=2**i
        else:
            LL.append(c)
            break

    #print(DP,LL)

    for x in LL:
        for j in range(N-x*l,-1,-1):
            if DP[j]!=1<<30:
                DP[j+x*l]=min(DP[j+x*l],DP[j]+x)

    #print(DP)

for i in range(1,N+1):
    if DP[i]==1<<30:
        print(-1)
    else:
        print(DP[i]-1)
0