結果

問題 No.2820 Non-Preferred IUPAC Nomenclature
ユーザー titia
提出日時 2024-07-27 05:31:33
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
MLE  
実行時間 -
コード長 1,364 bytes
コンパイル時間 555 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 681,328 KB
最終ジャッジ日時 2024-07-27 05:31:40
合計ジャッジ時間 6,070 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other MLE * 1 -- * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

from operator import itemgetter

N=int(input())
C=[input().split() for i in range(N)]

E=[[] for i in range(N)]


for i in range(N):
    for j in range(4):
        if C[i][j]=="H":
            continue
        x=int(C[i][j])-1
        E[i].append(x)
        
# 木のHL分解+LCA

ROOT=0

QUE=[ROOT] 
Parent=[-1]*N
Parent[ROOT]=N # ROOTの親を定めておく.
Child=[[] for i in range(N)]
TOP_SORT=[] # トポロジカルソート

while QUE: # トポロジカルソートと同時に親を見つける
    x=QUE.pop()
    TOP_SORT.append(x)
    for to in E[x]:
        if Parent[to]==-1:
            Parent[to]=x
            Child[x].append(to)
            QUE.append(to)

Children=[1]*N

for x in TOP_SORT[::-1]: #(自分を含む)子ノードの数を調べる
    if x==ROOT:
        break
    Children[Parent[x]]+=Children[x]

DP=[[] for i in range(N)]

for x in TOP_SORT[::-1]:
    #print(DP)
    if Child[x]==[]:
        DP[x]=["(methyl)"]
    else:
        LIST=[]
        for c in Child[x]:
            LIST.append((Children[c],c))

        LIST.sort(key=itemgetter(0),reverse=True)

        NOW=["("]
        for _,c in LIST:
            NOW+=DP[c]

        NOW.append("methyl")
        NOW.append(")")

        DP[x]=NOW

ANS=DP[0][1:]
ANS.pop()
ANS[-1]="methane"

print("".join(ANS))

        
            
0