結果

問題 No.2820 Non-Preferred IUPAC Nomenclature
コンテスト
ユーザー InTheBloom
提出日時 2024-07-26 22:23:04
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 722 ms / 2,000 ms
+ 27µs
コード長 1,492 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,146 ms
コンパイル使用メモリ 176,052 KB
実行使用メモリ 40,516 KB
最終ジャッジ日時 2026-11-01 19:13:58
合計ジャッジ時間 10,076 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main () {
    int N; cin >> N;
    vector<vector<int>> graph(N);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < 4; j++) {
            string str; cin >> str;
            int to;
            try {
                to = stoi(str) - 1;
            }
            catch (...) {
                continue;
            }

            graph[i].push_back(to);
        }
    }

    // それぞれのノードで名前を構築するとしんどいので、dfs巡回順だけ見て、各自の受け持ち分だけ構築する。
    vector<string> prefix(N), suffix(N);

    {
        auto dfs = [&] (auto self, int pos, int par) -> void {
            bool has = false;
            for (auto nex: graph[pos]) {
                if (nex == par) continue;
                self(self, nex, pos);
            }

            if (par != -1) {
                prefix[pos] = "(";
                suffix[pos] = "methyl)";
            }
            else {
                suffix[pos] = "methane";
            }
        };

        dfs(dfs, 0, -1);
    }

    string ans = "";
    {
        auto dfs = [&] (auto self, int pos, int par) -> void {
            ans += prefix[pos];

            for (auto nex: graph[pos]) {
                if (nex == par) continue;
                self(self, nex, pos);
            }

            ans += suffix[pos];
        };

        dfs(dfs, 0, -1);
    }

    cout << ans << "\n";
}
0