結果

問題 No.898 tri-βutree
ユーザー nasadigitalnasadigital
提出日時 2019-10-04 22:37:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,952 bytes
コンパイル時間 1,611 ms
コンパイル使用メモリ 168,496 KB
実行使用メモリ 29,528 KB
最終ジャッジ日時 2024-04-14 11:15:11
合計ジャッジ時間 12,881 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:71:35: warning: 'f' may be used uninitialized [-Wmaybe-uninitialized]
   71 |         int smallTree = findParent(f, s);
      |                         ~~~~~~~~~~^~~~~~
main.cpp:64:13: note: 'f' was declared here
   64 |         int f, s, t;
      |             ^
main.cpp:71:35: warning: 's' may be used uninitialized [-Wmaybe-uninitialized]
   71 |         int smallTree = findParent(f, s);
      |                         ~~~~~~~~~~^~~~~~
main.cpp:64:16: note: 's' was declared here
   64 |         int f, s, t;
      |                ^
In function 'll pathSum(int, int, int)',
    inlined from 'int main()' at main.cpp:73:51:
main.cpp:38:26: warning: 't' may be used uninitialized [-Wmaybe-uninitialized]
   38 |     return tot[a] + tot[b] - 2 * tot[p];
      |                     ~~~~~^
main.cpp: In function 'int main()':
main.cpp:64:19: note: 't' was declared here
   64 |         int f, s, t;
      |                   ^

ソースコード

diff #

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

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[a] > lvl[b]);
        swap(a, b);
    for (int ctr1 = 0; ctr1 < LOG_SIZE; ++ctr1)
        if ((lvl[b] - lvl[a]) & (1 << ctr1))
            b = lcs[b][ctr1];
    if (a == b)
        return a;
    for (int ctr1 = lvl[a] - 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];

    cin >> q;
    for (int ctr1 = 0; ctr1 < q; ++ctr1) {
        cin >> a >> b >> c;
        int ab = findParent(a, b);
        int bc = findParent(b, c);
        int ac = findParent(a, c);
        int f, s, t;
        if (ab == bc)
            f = a, s = c, t = b;
        if (bc == ac)
            f = a, s = b, t = c;
        if (ab == ac)
            f = b, s = c, t = a;
        int smallTree = findParent(f, s);
        int bigTree = findParent(smallTree, t);
        cout << pathSum(f, s, smallTree) + pathSum(smallTree, t, bigTree) << "\n";
    }

    return 0;
}
0