結果

問題 No.2820 Non-Preferred IUPAC Nomenclature
コンテスト
ユーザー D M
提出日時 2026-02-05 10:32:52
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 1,454 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,412 ms
コンパイル使用メモリ 341,520 KB
実行使用メモリ 11,704 KB
最終ジャッジ日時 2026-02-05 10:33:04
合計ジャッジ時間 10,075 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct Act {
    int type;     // 0: node, 1: char, 2: suffix
    int u, p;
    bool is_root;
    char ch;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vector<array<int,4>> C(n);
    for (int i = 0; i < n; i++) {
        for (int k = 0; k < 4; k++) {
            string s; cin >> s;
            if (s == "H") C[i][k] = -1;
            else C[i][k] = (int)stoll(s) - 1;  // stoll 推奨
        }
    }

    vector<Act> st;
    st.push_back({0, 0, -1, true, 0}); // root を 0(=炭素1)に決める

    while (!st.empty()) {
        Act a = st.back();
        st.pop_back();

        if (a.type == 1) {
            cout << a.ch;
            continue;
        }
        if (a.type == 2) {
            cout << (a.is_root ? "methane" : "methyl");
            continue;
        }

        // node a.u を展開:
        // ( child )( child ) ... + suffix を出す
        st.push_back({2, a.u, a.p, a.is_root, 0}); // 最後に語尾

        // LIFO なので、子を逆順に積む(順番は自由なのでどれでもOK)
        for (int k = 3; k >= 0; k--) {
            int v = C[a.u][k];
            if (v < 0 || v == a.p) continue;

            st.push_back({1, 0, 0, false, ')'});
            st.push_back({0, v, a.u, false, 0});
            st.push_back({1, 0, 0, false, '('});
        }
    }
    cout << '\n';
}
0