結果

問題 No.2820 Non-Preferred IUPAC Nomenclature
ユーザー Tatsu_mrTatsu_mr
提出日時 2024-07-27 10:50:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 506 ms / 2,000 ms
コード長 4,203 bytes
コンパイル時間 2,788 ms
コンパイル使用メモリ 219,688 KB
実行使用メモリ 102,024 KB
最終ジャッジ日時 2024-07-27 10:50:47
合計ジャッジ時間 9,199 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 506 ms
102,024 KB
testcase_05 AC 247 ms
54,952 KB
testcase_06 AC 468 ms
89,680 KB
testcase_07 AC 43 ms
13,064 KB
testcase_08 AC 17 ms
8,360 KB
testcase_09 AC 27 ms
10,900 KB
testcase_10 AC 5 ms
6,940 KB
testcase_11 AC 3 ms
6,944 KB
testcase_12 AC 4 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct Edge {
    Edge() {}
    Edge(int from_, int to_, long long cost_, int idx_) : from(from_), to(to_), cost(cost_), idx(idx_) {}
    
    int from, to;
    long long cost;
    int idx;
};

struct Graph {
    private:
    int n, m;
    bool dir;
    vector<vector<Edge>> g;
    vector<Edge> e;
    
    public:
    Graph(int n_, bool dir_) : n(n_), m(0), dir(dir_), g(n), e(0) {}
    Graph(int n_) : n(n_), m(0), dir(false), g(n), e(0) {}
    
    int size() {
        return n;
    }
    
    int edgesize() {
        return m;
    }
    
    bool directed() {
        return dir;
    }
    
    void add(int u, int v, long long w) {
        g[u].emplace_back(u, v, w, m);
        e.emplace_back(u, v, w, m);
        if (!dir) {
            g[v].emplace_back(v, u, w, m);
        }
        m++;
    }
    
    vector<Edge> operator[](int v) {
        return g[v];
    }
    
    Edge edge(int i) {
        return e[i];
    }
};

struct Tree {
    private:
    int n;
    Graph g;
    vector<vector<int>> parent;
    vector<int> depth;
    vector<long long> distance;
    int root;
    
    void build() {
        queue<int> q;
        depth[root] = 0;
        distance[root] = 0LL;
        q.push(root);
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (auto e : g[v]) {
                int nv = e.to;
                long long d = e.cost;
                if (depth[nv] == -1) {
                    depth[nv] = depth[v] + 1;
                    distance[nv] = distance[v] + d;
                    parent[nv][0] = v;
                    q.push(nv);
                }
            }
        }
        for (int j = 1; j < 30; j++) {
            for (int i = 0; i < n; i++) {
                parent[i][j] = parent[parent[i][j - 1]][j - 1];
            }
        }
    }
    
    public:
    Tree(Graph g_) : n(g_.size()), g(g_), parent(n, vector<int>(30)), depth(n, -1), distance(n, -1LL), root(0) {
        build();
    }
    Tree(Graph g_, int root_) :n(g_.size()), g(g_), parent(n, vector<int>(30)), depth(n, -1), distance(n, -1LL), root(root_) {
        build();
    }
    
    int lca(int u, int v) {
        if (dep(u) < dep(v)) {
            swap(u, v);
        }
        u = la(u, dep(u) - dep(v));
        if (u == v) {
            return u;
        }
        for (int j = 29; j >= 0; j--) {
            if (parent[u][j] != parent[v][j]) {
                u = parent[u][j];
                v = parent[v][j];
            }
        }
        return par(u);
    }
    
    int la(int v, int k) {
        int res = v;
        for (int j = 0; j < 30; j++) {
            if (k & (1 << j)) {
                res = parent[res][j];
            }
        }
        return res;
    }
    
    int jump(int u, int v, int i) {
        int l = lca(u, v);
        int cnt = dep(u) + dep(v) - 2 * dep(l);
        if (cnt < i) {
            return -1;
        }
        if (i <= dep(u) - dep(l)) {
            return la(u, i);
        } else {
            return la(v, cnt - i);
        }
    }
    
    long long dist(int u, int v) {
        int l = lca(u, v);
        return distance[u] + distance[v] - 2 * distance[l];
    }
    
    int par(int v) {
        return parent[v][0];
    }
    
    int dep(int v) {
        return depth[v];
    }
    
    bool onpath(int u, int v, int w) {
        return dist(u, w) + dist(v, w) == dist(u, v);
    }
};

int main() {
    int n;
    cin >> n;
    Graph g(n);
    for (int u = 0; u < n; u++) {
        for (int i = 0; i < 4; i++) {
            string s;
            cin >> s;
            if (s != "H") {
                int v = stoi(s) - 1;
                if (u < v) {
                    g.add(u, v, 1LL);
                }
            }
        }
    }
    Tree tr(g);
    string ans = "";
    auto f = [&](auto f, int v) -> void {
        string res = "";
        for (auto e : g[v]) {
            int nv = e.to;
            if (tr.dep(nv) < tr.dep(v)) {
                continue;
            }
            ans += "(";
            f(f, nv);
            ans += "yl)";
        }
        ans += "meth";
    };
    f(f, 0);
    ans += "ane";
    cout << ans << endl;
}
0