結果

問題 No.2820 Non-Preferred IUPAC Nomenclature
ユーザー InTheBloom
提出日時 2024-07-26 22:22:14
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,491 bytes
コンパイル時間 2,754 ms
コンパイル使用メモリ 96,068 KB
実行使用メモリ 36,248 KB
最終ジャッジ日時 2024-07-26 22:22:21
合計ジャッジ時間 7,672 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 4
other WA * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#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] = "methan";
            }
        };

        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