結果

問題 No.1676 Coin Trade (Single)
ユーザー taiga0629kyoprotaiga0629kyopro
提出日時 2021-09-10 22:42:34
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 534 ms / 2,000 ms
コード長 1,475 bytes
コンパイル時間 669 ms
コンパイル使用メモリ 86,984 KB
実行使用メモリ 148,932 KB
最終ジャッジ日時 2023-09-02 19:38:46
合計ジャッジ時間 10,621 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,192 KB
testcase_01 AC 76 ms
71,444 KB
testcase_02 AC 75 ms
71,252 KB
testcase_03 AC 435 ms
126,936 KB
testcase_04 AC 416 ms
118,480 KB
testcase_05 AC 398 ms
116,828 KB
testcase_06 AC 387 ms
115,796 KB
testcase_07 AC 343 ms
107,216 KB
testcase_08 AC 236 ms
86,744 KB
testcase_09 AC 341 ms
106,160 KB
testcase_10 AC 349 ms
108,760 KB
testcase_11 AC 454 ms
129,392 KB
testcase_12 AC 294 ms
98,008 KB
testcase_13 AC 76 ms
71,432 KB
testcase_14 AC 74 ms
71,040 KB
testcase_15 AC 75 ms
71,336 KB
testcase_16 AC 74 ms
71,352 KB
testcase_17 AC 74 ms
71,040 KB
testcase_18 AC 75 ms
71,008 KB
testcase_19 AC 77 ms
71,232 KB
testcase_20 AC 76 ms
71,048 KB
testcase_21 AC 77 ms
71,256 KB
testcase_22 AC 76 ms
71,344 KB
testcase_23 AC 73 ms
71,412 KB
testcase_24 AC 75 ms
71,204 KB
testcase_25 AC 78 ms
71,040 KB
testcase_26 AC 76 ms
71,436 KB
testcase_27 AC 77 ms
71,304 KB
testcase_28 AC 75 ms
71,484 KB
testcase_29 AC 76 ms
71,416 KB
testcase_30 AC 76 ms
71,404 KB
testcase_31 AC 78 ms
71,048 KB
testcase_32 AC 75 ms
71,240 KB
testcase_33 AC 534 ms
148,112 KB
testcase_34 AC 527 ms
146,996 KB
testcase_35 AC 526 ms
148,932 KB
testcase_36 AC 523 ms
147,376 KB
testcase_37 AC 530 ms
145,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class topological_sort():
    #1-indexed
    #0-indexedはバグる
    def __init__(self,n):
        self.n=n
        self.root=[[] for _ in range(self.n+1)]
        self.deg=[0]*(self.n+1)
    def add(self,a,b):
        #有向辺 a->b の追加
        self.root[a].append(b)
        self.deg[b]+=1
    def const(self):
        #トポロジカルソートした結果を返す
        #不可能なら [] を返す
        deg2=self.deg[:]
        search=[]
        res=[]
        for i in range(1,self.n+1):
            if deg2[i]==0:search.append(i)
        while search:
            now=search.pop()
            res.append(now)
            for next in self.root[now]:
                deg2[next]-=1
                if deg2[next]==0:
                    search.append(next)
        if len(res)==self.n:
            return res
        return []



n,k=map(int,input().split())

tp=topological_sort(2*n+10)

root=[[] for i in range(2*n+11)]
a=[0]*(n+1)
add=n+1
for i in range(1,n+1):
    x,m=map(int,input().split())
    a[i]=x
    b=list(map(int,input().split()))
    for y in b:
        root[y+add].append((i+add,0))
        tp.add(y+add,i+add)

for i in range(1,n+1):
    root[i].append((i+1,0))
    tp.add(i,i+1)
    root[i].append((i+add,-a[i]))
    tp.add(i,i+add)
    root[i+add].append((i+1,a[i]))
    tp.add(i+add,i+1)

dp=[0]*(2*n+11)
top=tp.const()
top.reverse()
for x in top:
    dp[x]=0
    for y,c in root[x]:
        dp[x]=max(dp[x],dp[y]+c)

print(dp[1])

0