結果

問題 No.317 辺の追加
ユーザー titiatitia
提出日時 2023-09-20 00:15:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 516 ms / 2,000 ms
コード長 1,258 bytes
コンパイル時間 549 ms
コンパイル使用メモリ 87,188 KB
実行使用メモリ 86,656 KB
最終ジャッジ日時 2023-09-20 00:15:53
合計ジャッジ時間 17,492 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 105 ms
71,732 KB
testcase_01 AC 90 ms
71,724 KB
testcase_02 AC 334 ms
81,768 KB
testcase_03 AC 341 ms
81,496 KB
testcase_04 AC 352 ms
82,752 KB
testcase_05 AC 354 ms
80,832 KB
testcase_06 AC 364 ms
81,392 KB
testcase_07 AC 229 ms
79,704 KB
testcase_08 AC 506 ms
83,644 KB
testcase_09 AC 357 ms
82,520 KB
testcase_10 AC 397 ms
82,636 KB
testcase_11 AC 259 ms
86,656 KB
testcase_12 AC 403 ms
81,988 KB
testcase_13 AC 292 ms
85,816 KB
testcase_14 AC 411 ms
82,932 KB
testcase_15 AC 403 ms
82,720 KB
testcase_16 AC 363 ms
83,332 KB
testcase_17 AC 407 ms
82,680 KB
testcase_18 AC 401 ms
82,284 KB
testcase_19 AC 406 ms
82,196 KB
testcase_20 AC 403 ms
83,232 KB
testcase_21 AC 191 ms
79,880 KB
testcase_22 AC 168 ms
79,216 KB
testcase_23 AC 181 ms
79,468 KB
testcase_24 AC 223 ms
81,092 KB
testcase_25 AC 235 ms
80,240 KB
testcase_26 AC 207 ms
79,976 KB
testcase_27 AC 197 ms
80,084 KB
testcase_28 AC 179 ms
79,508 KB
testcase_29 AC 150 ms
78,356 KB
testcase_30 AC 516 ms
81,320 KB
testcase_31 AC 410 ms
81,476 KB
testcase_32 AC 401 ms
81,212 KB
testcase_33 AC 425 ms
81,636 KB
testcase_34 AC 451 ms
81,612 KB
testcase_35 AC 423 ms
81,256 KB
testcase_36 AC 393 ms
81,220 KB
testcase_37 AC 440 ms
81,728 KB
testcase_38 AC 512 ms
81,176 KB
testcase_39 AC 403 ms
81,144 KB
権限があれば一括ダウンロードができます

ソースコード

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 C:
    c=C[l]

    LL=[]

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

    #print(c,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