結果

問題 No.898 tri-βutree
ユーザー 渡辺桜太渡辺桜太
提出日時 2023-12-16 09:24:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,057 bytes
コンパイル時間 868 ms
コンパイル使用メモリ 76,800 KB
実行使用メモリ 41,924 KB
最終ジャッジ日時 2023-12-16 09:25:02
合計ジャッジ時間 10,888 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 297 ms
41,924 KB
testcase_01 AC 5 ms
13,284 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<pair<int, int>> no[100000];
vector<int> o;
int d[100000], in[100000], out[100000], st[32][300000];

void tour(int n, int e) {
    d[n] = e;
    in[n] = o.size();
    o.push_back(n);
    for (auto i : no[n]) {
        if (d[i.first] == 0) {
            tour(i.first, e + i.second);
            o.push_back(n);
        }
    }
    out[n] = o.size();
}

int lca(int x, int y) {
    if (in[y] < in[x]) swap(x, y);
    int u = in[x], v = in[y];
    int i = 0;
    while (1 << (i + 1) <= v - u) i++;
    if (d[st[i][u]] < d[st[i][v-(1<<i)]]) return st[i][u];
    return st[i][v-(1<<i)];
}

int main() {
    int n, u, v, w, q, x, y, z;
    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        cin >> u >> v >> w;
        no[u].push_back({v, w});
        no[v].push_back({u, w});
    }
    tour(0, 1);
    for (int i = 0; i < o.size(); i++) {
        st[0][i] = o[i];
    }
    for (int i = 1; (1 << i) <= o.size(); i++) {
        for (int j = 0; j < o.size(); j++) {
            if (st[i -1][j] < st[i - 1][min(j + (1 << (i - 1)), (int)o.size() - 1)]) {
                st[i][j] = st[i - 1][j];
            } else {
                st[i][j] = st[i - 1][min(j + (1 << (i - 1)), (int)o.size() - 1)];
            }
        }
    }
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> x >> y >> z;
        u = lca(x, y);
        v = lca(y, z);
        w = lca(z, x);
        if (u == v) cout << d[x] + d[y] + d[z] - d[w] - 2 * d[u] << endl;
        else if (v == w) cout << d[x] + d[y] + d[z] - d[u] - 2 * d[v] << endl;
        else cout << d[x] + d[y] + d[z] - d[v] - 2 * d[u] << endl;
    }
    // for (int i = 0; i < o.size(); i++) cout << o[i] << " ";
    // cout << endl;
    // for (int i = 0; i < n; i++) cout << d[i] << " ";
    // cout << endl;
    // for (int i = 0; i < 4; i++) {
    //   for (int j = 0; j < o.size(); j++) {
    //     cout << st[i][j] << " ";
    //   }
    //   cout << endl;
    // }
    // cout << u << " " << v << " " << w << endl;
}
0