結果
| 問題 |
No.2820 Non-Preferred IUPAC Nomenclature
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-07-26 22:23:04 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,368 ms / 2,000 ms |
| コード長 | 1,492 bytes |
| コンパイル時間 | 1,193 ms |
| コンパイル使用メモリ | 96,912 KB |
| 実行使用メモリ | 36,232 KB |
| 最終ジャッジ日時 | 2024-07-26 22:23:15 |
| 合計ジャッジ時間 | 9,815 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 22 |
ソースコード
#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";
}