結果
問題 | No.2820 Non-Preferred IUPAC Nomenclature |
ユーザー |
![]() |
提出日時 | 2024-07-27 05:35:33 |
言語 | PyPy3 (7.3.15) |
結果 |
MLE
|
実行時間 | - |
コード長 | 1,470 bytes |
コンパイル時間 | 654 ms |
コンパイル使用メモリ | 82,492 KB |
実行使用メモリ | 844,896 KB |
最終ジャッジ日時 | 2024-07-27 05:35:39 |
合計ジャッジ時間 | 4,525 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | MLE * 1 -- * 21 |
ソースコード
import sysinput = sys.stdin.readlinefrom operator import itemgetterN=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":continuex=int(C[i][j])-1E[i].append(x)# 木のHL分解+LCAROOT=0QUE=[ROOT]Parent=[-1]*NParent[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]=xChild[x].append(to)QUE.append(to)Children=[1]*Nfor x in TOP_SORT[::-1]: #(自分を含む)子ノードの数を調べるif x==ROOT:breakChildren[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:if NOW==[]:NOW=DP[c]NOW[0]="("+NOW[0]else:NOW+=DP[c]NOW.append("methyl")NOW.append(")")DP[x]=NOWANS=DP[0][1:]ANS.pop()ANS[-1]="methane"print("".join(ANS))