結果

問題 No.2820 Non-Preferred IUPAC Nomenclature
ユーザー detteiuudetteiuu
提出日時 2024-07-26 22:28:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 679 ms / 2,000 ms
コード長 1,122 bytes
コンパイル時間 1,013 ms
コンパイル使用メモリ 81,948 KB
実行使用メモリ 229,432 KB
最終ジャッジ日時 2024-07-26 22:28:55
合計ジャッジ時間 8,919 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

from types import GeneratorType
from collections import deque

def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        to = f(*args, **kwargs)
        while True:
            if type(to) is GeneratorType:
                stack.append(to)
                to = next(to)
            else:
                stack.pop()
                if not stack:
                    break
                to = stack[-1].send(to)
        return to
    return wrappedfunc

N = int(input())
C = [list(input().split()) for _ in range(N)]
G = [[] for _ in range(N)]
for i in range(N):
    for j in range(4):
        if C[i][j] != "H":
            G[i].append(int(C[i][j])-1)

@bootstrap
def dfs(n):
    visited[n] = True
    for v in G[n]:
        if not visited[v]:
            A.append("(")
            yield dfs(v)
            A.append(")")
    yield

visited = [False]*N
A = []
dfs(0)

ans = []
for i in range(len(A)):
    if A[i] == "(":
        ans.append("(")
    else:
        ans.append("methyl")
        ans.append(")")
ans.append("methane")

print("".join(ans))
0