結果

問題 No.898 tri-βutree
ユーザー nasadigitalnasadigital
提出日時 2019-10-09 10:58:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,833 bytes
コンパイル時間 1,574 ms
コンパイル使用メモリ 171,276 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-27 16:14:31
合計ジャッジ時間 5,982 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
// #define MAX_SIZE 100000
// #define LOG_SIZE 17
#define MAX_SIZE 7
#define LOG_SIZE 4

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;

vector<ii> adjList[MAX_SIZE];
int lcs[MAX_SIZE][LOG_SIZE], lvl[MAX_SIZE];
ll tot[MAX_SIZE];

void dfs(int cur, ll sum, int p, int cd) {
    tot[cur] = sum;
    lcs[cur][0] = p;
    lvl[cur] = cd;
    for (auto nxt : adjList[cur])
        if (nxt.first != p)
            dfs(nxt.first, sum + nxt.second, cur, cd + 1);
}

int findParent(int a, int b) {
    if (lvl[b] < lvl[a])
        swap(a, b);
    int diff = lvl[b] - lvl[a];
    for (int ctr1 = 0; ctr1 < LOG_SIZE; ++ctr1)
        if (diff & (1 << ctr1))
            b = lcs[b][ctr1];
    if (a == b)
        return a;
    for (int ctr1 = LOG_SIZE - 1; ctr1 >= 0; --ctr1)
        if (lcs[a][ctr1] != lcs[b][ctr1])
            a = lcs[a][ctr1], b = lcs[b][ctr1];
    return lcs[a][0];
}

ll pathSum(int a, int b, int p) {
    return tot[a] + tot[b] - 2 * tot[p];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    cin >> n;

    int a, b, c;
    for (int ctr1 = 1; ctr1 < n; ++ctr1) {
        cin >> a >> b >> c;
        adjList[a].push_back({b, c});
        adjList[b].push_back({a, c});
    }
    dfs(0, 0, 0, 0);
    for (int ctr1 = 1; ctr1 < LOG_SIZE; ++ctr1)
        for (int ctr2 = 0; ctr2 < n; ++ctr2)
            lcs[ctr2][ctr1] = lcs[lcs[ctr2][ctr1 - 1]][ctr1 - 1];

    findParent(1, 3);
    cin >> q;
    int ch[3];
    for (int ctr1 = 0; ctr1 < q; ++ctr1) {
        for (int ctr2 = 0; ctr2 < 3; ++ctr2)
            cin >> ch[ctr2];
        ll rez = 0;
        for (int ctr2 = 0; ctr2 < 3; ++ctr2)
            rez += pathSum(ch[ctr2], ch[(ctr2 + 1) % 3], findParent(ch[ctr2], ch[(ctr2 + 1) % 3]));
        cout << rez / 2 << "\n";
    }

    return 0;
}
0