結果

問題 No.2820 Non-Preferred IUPAC Nomenclature
ユーザー Shirotsume
提出日時 2024-07-26 23:41:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,019 ms / 2,000 ms
コード長 1,270 bytes
コンパイル時間 388 ms
コンパイル使用メモリ 82,400 KB
実行使用メモリ 295,612 KB
最終ジャッジ日時 2024-07-26 23:41:44
合計ジャッジ時間 9,711 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, time, random
from collections import deque, Counter, defaultdict
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 61 - 1
mod = 998244353
sys.setrecursionlimit(2 * 10 ** 5 + 1)
n = ii()
graph = [[] for _ in range(n)]
for i in range(n):
    a, b, c, d = input().split()
    if a != 'H':
        a = int(a) - 1
        graph[i].append(a)
    if b != 'H':
        b = int(b) - 1
        graph[i].append(b)
    if c != 'H':
        c = int(c) - 1
        graph[i].append(c)
    if d != 'H':
        d = int(d) - 1
        graph[i].append(d)
        
from collections import deque
def TreeDepth(s, graph):
    inf = 2 ** 61 - 1
    n = len(graph)
    depth = [inf] * n
    depth[s] = 0
    q = deque()
    q.append(s)
    while q:
        now = q.popleft()
        for to in graph[now]:
            if depth[to] == inf:
                depth[to] = depth[now] + 1
                q.append(to)
    return depth

TD = TreeDepth(0, graph)
h = {}
cnt = 0
ans = []

def dfs(now):
    global ans
    for to in graph[now]:
        if TD[to] > TD[now]:
            ans.append('(')
            dfs(to)
            ans.append('methyl)')
dfs(0)
print(''.join(ans) + 'methane')   
0